package Parser
import Scanner "scanner"
+import Globals "globals"
+import Universe "universe"
export Parser
tok int; // one token look-ahead
beg, end int; // token position
ident string; // last ident seen
+ top_scope *Globals.Scope;
}
P.indent = 0;
P.S = S;
P.Next();
+ P.top_scope = Universe.scope;
}
}
+// ----------------------------------------------------------------------------
+
+func (P *Parser) OpenScope() {
+ P.top_scope = Globals.NewScope(P.top_scope);
+}
+
+
+func (P *Parser) CloseScope() {
+ P.top_scope = P.top_scope.parent;
+}
+
+
+func (P *Parser) Lookup(ident string) *Globals.Object {
+ for scope := P.top_scope; scope != nil; scope = scope.parent {
+ obj := scope.Lookup(ident);
+ if obj != nil {
+ return obj;
+ }
+ }
+ return nil;
+}
+
+
+func (P *Parser) DeclareInScope(scope *Globals.Scope, obj *Globals.Object) {
+ if scope.Lookup(obj.ident) != nil {
+ // TODO is this the correct error position?
+ P.Error(P.beg, `"` + obj.ident + `" is declared already`);
+ return; // don't insert it into the scope
+ }
+ scope.Insert(obj);
+}
+
+
+func (P *Parser) Declare(obj *Globals.Object) {
+ P.DeclareInScope(P.top_scope, obj);
+}
+
+
+// ----------------------------------------------------------------------------
+
func (P *Parser) TryType() bool;
func (P *Parser) ParseExpression();
P.Trace("InterfaceType");
P.Expect(Scanner.INTERFACE);
P.Expect(Scanner.LBRACE);
+ P.OpenScope();
for P.tok != Scanner.RBRACE {
P.ParseMethodDecl();
}
+ P.CloseScope();
P.Next();
P.Ecart();
}
P.Trace("StructType");
P.Expect(Scanner.STRUCT);
P.Expect(Scanner.LBRACE);
+ P.OpenScope();
for P.tok != Scanner.RBRACE {
P.ParseFieldDecl();
if P.tok != Scanner.RBRACE {
}
}
P.Optional(Scanner.SEMICOLON);
+ P.CloseScope();
P.Expect(Scanner.RBRACE);
P.Ecart();
}
func (P *Parser) ParseAnonymousSignature() {
P.Trace("AnonymousSignature");
+ P.OpenScope();
P.ParseParameters();
if P.tok == Scanner.PERIOD {
P.Next();
P.ParseParameters();
}
P.TryResult();
+ P.CloseScope();
P.Ecart();
}
func (P *Parser) ParseNamedSignature() {
P.Trace("NamedSignature");
+ P.OpenScope();
if P.tok == Scanner.LPAREN {
P.ParseParameters();
}
P.ParseIdent(); // function name
P.ParseParameters();
P.TryResult();
+ P.CloseScope();
P.Ecart();
}
func (P *Parser) ParseIfStat() {
P.Trace("IfStat");
P.Expect(Scanner.IF);
+ P.OpenScope();
if P.tok != Scanner.LBRACE {
if P.tok != Scanner.SEMICOLON {
P.ParseSimpleStat();
P.ParseStatement();
}
}
+ P.CloseScope();
P.Ecart();
}
func (P *Parser) ParseForStat() {
P.Trace("ForStat");
P.Expect(Scanner.FOR);
+ P.OpenScope();
if P.tok != Scanner.LBRACE {
if P.tok != Scanner.SEMICOLON {
P.ParseSimpleStat();
}
}
P.ParseBlock();
+ P.CloseScope();
P.Ecart();
}
func (P *Parser) ParseSwitchStat() {
P.Trace("SwitchStat");
P.Expect(Scanner.SWITCH);
+ P.OpenScope();
if P.tok != Scanner.LBRACE {
if P.tok != Scanner.SEMICOLON {
P.ParseSimpleStat();
P.ParseCaseClause();
}
P.Expect(Scanner.RBRACE);
+ P.CloseScope();
P.Ecart();
}
func (P *Parser) ParseBlock() {
P.Trace("Block");
P.Expect(Scanner.LBRACE);
+ P.OpenScope();
if P.tok != Scanner.RBRACE && P.tok != Scanner.SEMICOLON {
P.ParseStatementList();
}
P.Optional(Scanner.SEMICOLON);
+ P.CloseScope();
P.Expect(Scanner.RBRACE);
P.Ecart();
}
}
+func (P *Parser) ParseFunctionLit() {
+ P.Trace("FunctionLit");
+ P.ParseFunctionType();
+ P.ParseBlock();
+ P.Ecart();
+}
+
+
func (P *Parser) ParseOperand() {
P.Trace("Operand");
switch P.tok {
case Scanner.TRUE: fallthrough;
case Scanner.FALSE:
P.Next();
+ case Scanner.FUNC:
+ P.ParseFunctionLit();
case Scanner.NEW:
P.ParseNew();
default:
func (P *Parser) ParseProgram() {
P.Trace("Program");
+ P.OpenScope();
P.Expect(Scanner.PACKAGE);
P.ParseIdent();
P.Optional(Scanner.SEMICOLON);
- for P.tok == Scanner.IMPORT {
- P.ParseImportDecl();
- P.Optional(Scanner.SEMICOLON);
+ { P.OpenScope();
+ for P.tok == Scanner.IMPORT {
+ P.ParseImportDecl();
+ P.Optional(Scanner.SEMICOLON);
+ }
+
+ for P.tok != Scanner.EOF {
+ P.ParseDeclaration();
+ P.Optional(Scanner.SEMICOLON);
+ }
+ P.CloseScope();
}
- for P.tok != Scanner.EOF {
- P.ParseDeclaration();
- P.Optional(Scanner.SEMICOLON);
- }
+ P.CloseScope();
P.Ecart();
}
var Keywords *map [string] int;
+var VerboseMsgs bool; // error message customization
export TokenName
}
+func IsUser(username string) bool {
+ for i := 0; i < sys.envc(); i++ {
+ if sys.envv(i) == "USER=" + username {
+ return true;
+ }
+ }
+ return false;
+}
+
+
func Init () {
Keywords = new(map [string] int);
for i := KEYWORDS_BEG; i <= KEYWORDS_END; i++ {
Keywords[TokenName(i)] = i;
}
+
+ // r doesn't want column information in error messages...
+ VerboseMsgs = !IsUser("r");
}
const errdist = 10;
if pos > S.errpos + errdist || S.nerrors == 0 {
line, col := S.LineCol(pos);
- print S.filename, ":", line, ":", col, ": ", msg, "\n";
+ if VerboseMsgs {
+ print S.filename, ":", line, ":", col, ": ", msg, "\n";
+ } else {
+ print S.filename, ":", line, ": ", msg, "\n";
+ }
S.nerrors++;
S.errpos = pos;
}