Attach parser location information to AST nodes.
[goals.git] / src / parser.mly
index 4cb18af..69cb063 100644 (file)
@@ -36,11 +36,9 @@ open Printf
 %token RIGHT_PAREN
 %token <Ast.substs> STRING
 
-%type <Ast.expr> expr
-%type <Ast.stmt> stmt
-
-(* Start non-terminal. *)
-%start <Ast.file> file
+(* Start nonterminals. *)
+%start <Ast.env> file
+%start <Ast.expr> expr
 %%
 
 file:
@@ -48,24 +46,27 @@ file:
     ;
 
 stmts:
-    | list(stmt) { $1 }
+    | list(stmt)
+    { List.fold_left (
+        fun env (name, expr) -> Ast.StringMap.add name expr env
+      ) Ast.StringMap.empty $1
+    }
     ;
 stmt:
     | option(goal_stmt) patterns COLON barelist option(CODE)
     { let name, params =
         match $1 with
         | None ->
-           let pos = $startpos in
-           sprintf "_goal@%d" pos.pos_lnum, []
+           sprintf "_goal@%d" $startpos.pos_lnum, []
         | Some x -> x in
-      Ast.Goal (name, params, $2, $4, $5)
+      name, Ast.EGoal ($loc, (params, $2, $4, $5))
     }
     | goal_stmt CODE
     {
       let name, params = $1 in
-      Ast.Goal (name, params, [], [], Some $2)
+      name, Ast.EGoal ($loc, (params, [], [], Some $2))
     }
-    | LET ID EQUALS expr { Ast.Let ($2, $4) }
+    | LET ID EQUALS expr { $2, $4 }
     ;
 
 goal_stmt:
@@ -80,9 +81,9 @@ patterns:
     | separated_list(COMMA, pattern) { $1 }
     ;
 pattern:
-    | STRING     { Ast.PTactic ("file", [$1]) }
-    | ID pattern_params { Ast.PTactic ($1, $2) }
-    | ID         { Ast.PVarSubst $1 }
+    | STRING     { Ast.PTactic ($loc, "file", [$1]) }
+    | ID pattern_params { Ast.PTactic ($loc, $1, $2) }
+    | ID         { Ast.PVar ($loc, $1) }
     ;
 pattern_params:
     | LEFT_PAREN separated_list(COMMA, pattern_param) RIGHT_PAREN { $2 }
@@ -92,10 +93,10 @@ pattern_param:
     ;
 
 expr:
-    | ID params  { Ast.ECall ($1, $2) }
-    | ID         { Ast.EVar $1 (* This might be replaced with ECall later. *) }
-    | STRING     { Ast.EString $1 }
-    | LEFT_ARRAY barelist RIGHT_ARRAY { Ast.EList $2 }
+    | ID params  { Ast.ECall ($loc, $1, $2) }
+    | ID         { Ast.EVar ($loc, $1) }
+    | STRING     { Ast.ESubsts ($loc, $1) }
+    | LEFT_ARRAY barelist RIGHT_ARRAY { Ast.EList ($loc, $2) }
     ;
 barelist:
     | separated_list(COMMA, expr) { $1 }