Anonymous avatar Anonymous committed 7dae9f8

Fill in lineno and column in AST nodes.

Comments (0)

Files changed (10)

 
 /* --- Factory functions for all the different nodes ------------------------ */
 
+/* AR_NODE_SIG contains AR_SIG, filename, lineno, col */
 
 #define AR_TOPLEVEL_NODE_CREATE(i)                                      \
     ArToplevelNode *node = (ArToplevelNode *)AR_ALLOCT(ArToplevelNode); \
     if (!node)                                                          \
         AR_RAISE_OOM();                                                 \
-    node->type = AR_AST_##i
+    node->type = AR_AST_##i;                                            \
+    node->lineno = lineno;                                              \
+    node->col = col 
 
 ArToplevelNode *
-arana_ast_Module(AR_SIG, ArStmtSeq *stmts)
+arana_ast_Module(AR_NODE_SIG, ArStmtSeq *stmts)
 {
     AR_TOPLEVEL_NODE_CREATE(Module);
     node->v.Module.stmts = stmts;
     ArStmtNode *node = (ArStmtNode *)AR_ALLOCT(ArStmtNode);      \
     if (!node)                                                   \
         AR_RAISE_OOM();                                          \
-    node->type = AR_AST_##i
+    node->type = AR_AST_##i;                                     \
+    node->lineno = lineno;                                       \
+    node->col = col 
 
 ArStmtNode *
-arana_ast_Compound(AR_SIG, ArStmtSeq *stmts)
+arana_ast_Compound(AR_NODE_SIG, ArStmtSeq *stmts)
 {
     AR_STMT_NODE_CREATE(Compound);
     if (!stmts) {
 }
 
 ArStmtNode *
-arana_ast_ExprStmt(AR_SIG, ArExprNode *expr)
+arana_ast_ExprStmt(AR_NODE_SIG, ArExprNode *expr)
 {
     AR_STMT_NODE_CREATE(ExprStmt);
     node->v.ExprStmt.expr = expr;
 }
 
 ArStmtNode *
-arana_ast_If(AR_SIG, ArExprNode *test, ArStmtNode *body, ArStmtNode *else_)
+arana_ast_If(AR_NODE_SIG, ArExprNode *test, ArStmtNode *body, ArStmtNode *else_)
 {
     AR_STMT_NODE_CREATE(If);
     node->v.If.test = test;
 }
 
 ArStmtNode *
-arana_ast_For(AR_SIG, ArExprNode *target, ArExprNode *seq, ArStmtNode *body)
+arana_ast_For(AR_NODE_SIG, ArExprNode *target, ArExprNode *seq, ArStmtNode *body)
 {
     AR_STMT_NODE_CREATE(For);
     node->v.For.target = target;
 }
 
 ArStmtNode *
-arana_ast_Def(AR_SIG, const char *name, ArExprSeq *args,
+arana_ast_Def(AR_NODE_SIG, const char *name, ArExprSeq *args,
               ArExprSeq *defaults, Ar_size_t arg_count,
               Ar_size_t default_count, ArStmtNode *body)
 {
     ArExprNode *node = (ArExprNode *)AR_ALLOCT(ArExprNode);      \
     if (!node)                                                   \
         AR_RAISE_OOM();                                          \
-    node->type = AR_AST_##i
+    node->type = AR_AST_##i;                                     \
+    node->lineno = lineno;                                       \
+    node->col = col 
 
 #define AR_EXPR_NODE_INIT1(i, n)                                 \
     AR_EXPR_NODE_CREATE(i);                                      \
     return node
 
 ArExprNode *
-arana_ast_Assign(AR_SIG, ArExprNode *target, ArExprNode *expr)
+arana_ast_Assign(AR_NODE_SIG, ArExprNode *target, ArExprNode *expr)
 {
     AR_EXPR_NODE_INIT2(Assign, target, expr);
 }
 
 ArExprNode *
-arana_ast_AugAssign(AR_SIG, ArExprNode *target, ArExprNode *expr, ArAstOp op)
+arana_ast_AugAssign(AR_NODE_SIG, ArExprNode *target, ArExprNode *expr, ArAstOp op)
 {
     AR_EXPR_NODE_INIT3(AugAssign, target, expr, op);
 }
 
 ArExprNode *
-arana_ast_BinaryOp(AR_SIG, ArExprNode *left, ArExprNode *right, ArAstOp op)
+arana_ast_BinaryOp(AR_NODE_SIG, ArExprNode *left, ArExprNode *right, ArAstOp op)
 {
     AR_EXPR_NODE_INIT3(BinaryOp, left, right, op);
 }
 
 ArExprNode *
-arana_ast_UnaryOp(AR_SIG, ArExprNode *expr, ArAstOp op)
+arana_ast_UnaryOp(AR_NODE_SIG, ArExprNode *expr, ArAstOp op)
 {
     AR_EXPR_NODE_INIT2(UnaryOp, expr, op);
 }
 
 ArExprNode *
-arana_ast_Call(AR_SIG, ArExprNode *expr, ArExprNode *args)
+arana_ast_Call(AR_NODE_SIG, ArExprNode *expr, ArExprNode *args)
 {
     AR_EXPR_NODE_INIT2(Call, expr, args);
 }
 
 ArExprNode *
-arana_ast_Attribute(AR_SIG, ArExprNode *expr, ArExprNode *attr, ArAstCtx ctx)
+arana_ast_Attribute(AR_NODE_SIG, ArExprNode *expr, ArExprNode *attr, ArAstCtx ctx)
 {
     AR_EXPR_NODE_INIT3(Attribute, expr, attr, ctx);
 }
 
 ArExprNode *
-arana_ast_Subscript(AR_SIG, ArExprNode *expr, ArExprNode *index, ArAstCtx ctx)
+arana_ast_Subscript(AR_NODE_SIG, ArExprNode *expr, ArExprNode *index, ArAstCtx ctx)
 {
     AR_EXPR_NODE_INIT3(Subscript, expr, index, ctx);
 }
 
 ArExprNode *
-arana_ast_List(AR_SIG, ArExprSeq *items)
+arana_ast_List(AR_NODE_SIG, ArExprSeq *items)
 {
     AR_EXPR_NODE_INIT1(List, items);
 }
 
 ArExprNode *
-arana_ast_Tuple(AR_SIG, ArExprSeq *items, ArAstCtx ctx)
+arana_ast_Tuple(AR_NODE_SIG, ArExprSeq *items, ArAstCtx ctx)
 {
     AR_EXPR_NODE_INIT2(Tuple, items, ctx);
 }
 
 ArExprNode *
-arana_ast_Interpolation(AR_SIG, ArExprSeq *items)
+arana_ast_Interpolation(AR_NODE_SIG, ArExprSeq *items)
 {
     AR_EXPR_NODE_INIT1(Interpolation, items);
 }
 
 ArExprNode *
-arana_ast_Name(AR_SIG, const char *ident, ArAstCtx ctx)
+arana_ast_Name(AR_NODE_SIG, const char *ident, ArAstCtx ctx)
 {
     AR_EXPR_NODE_INIT2(Name, ident, ctx);
 }
 
 ArExprNode *
-arana_ast_Literal(AR_SIG, AR value)
+arana_ast_Literal(AR_NODE_SIG, AR value)
 {
     AR_EXPR_NODE_INIT1(Literal, value);
 }
     default: {
         char buf[100];
         snprintf(buf, 100, "can't assign to %s", AR_ASTMAP[node->type]);
-        AR_RAISE(AR_SYNTAX_ERROR(buf, AR_STRING(node->filename), node->lineno, 0));
+        AR_RAISE(AR_SYNTAX_ERROR(buf, NULL, node->lineno, 0));  // XXX filename?
     }
     }
 }
 }
 
 
-static int
-_arana_ast_set_filename_cb(AR_SIG, ArAstNode *node, void *closure)
-{
-    node->filename = (char *)closure;
-    return 1;
-}
-
-void
-arana_ast_set_filename(AR_SIG, ArAstNode *node, const char *filename)
-{
-    arana_ast_visit(AR_ISIG, node, _arana_ast_set_filename_cb, NULL, (void *)filename);
-}
-
-
 /* --- AST optimizing ------------------------------------------------------- */
 
 static int
 #ifndef _ARANA_AST_H
 #define _ARANA_HAS_H
 
-#define AR_AST_NODE_HEAD const char *filename; int lineno; ArAstType type;
+#define AR_AST_NODE_HEAD int lineno; int col; ArAstType type;
 #define AR_AST_BINARY_HEAD AR_AST_NODE_HEAD; ArAstNode *left, ArAstNode *right;
-#define AR_AST(type, ...) arana_ast_##type(AR_ISIG, __VA_ARGS__)
+#define AR_AST(type, ...) arana_ast_##type(AR_ISIG, 0, 0, __VA_ARGS__)
 #define AR_AST_SET_STORE_CTX(node) arana_ast_set_store_ctx(AR_ISIG, node)
 #define AR_AST_TYPE(x) ((ArAstNode *)(x))->type
 #define AR_AST_IS(x, t) (AR_AST_TYPE(x) == AR_AST_##t)
         target = seq;                                                   \
     }
 
-#define AR_AST_MAYBE_TUPLE(target, seq, ctx) {                          \
-        if (seq->count == 1) {                                          \
-            target = seq->items[0];                                     \
-        } else {                                                        \
-            target = AR_AST(Tuple, seq, ctx);                           \
-        }                                                               \
-    }
-
 typedef enum _ArAstType {
     AR_AST_Module,
 
 
 typedef int (*ArAstVisitFunc)(AR_SIG, ArAstNode *node, void *closure);
 
-AR_API_FUNC(ArToplevelNode *) arana_ast_Module(AR_SIG, ArStmtSeq *stmts);
 
-AR_API_FUNC(ArStmtNode *) arana_ast_Compound(AR_SIG, ArStmtSeq *stmts);
-AR_API_FUNC(ArStmtNode *) arana_ast_ExprStmt(AR_SIG, ArExprNode *expr);
-AR_API_FUNC(ArStmtNode *) arana_ast_If(AR_SIG, ArExprNode *test,
+#define AR_NODE_SIG AR_SIG, int lineno, int col
+
+AR_API_FUNC(ArToplevelNode *) arana_ast_Module(AR_NODE_SIG, ArStmtSeq *stmts);
+
+AR_API_FUNC(ArStmtNode *) arana_ast_Compound(AR_NODE_SIG, ArStmtSeq *stmts);
+AR_API_FUNC(ArStmtNode *) arana_ast_ExprStmt(AR_NODE_SIG, ArExprNode *expr);
+AR_API_FUNC(ArStmtNode *) arana_ast_If(AR_NODE_SIG, ArExprNode *test,
                                        ArStmtNode *body, ArStmtNode *else_);
-AR_API_FUNC(ArStmtNode *) arana_ast_For(AR_SIG, ArExprNode *target,
+AR_API_FUNC(ArStmtNode *) arana_ast_For(AR_NODE_SIG, ArExprNode *target,
                                         ArExprNode *seq, ArStmtNode *body);
-AR_API_FUNC(ArStmtNode *) arana_ast_Def(AR_SIG, const char *name,
+AR_API_FUNC(ArStmtNode *) arana_ast_Def(AR_NODE_SIG, const char *name,
                                         ArExprSeq *args,
                                         ArExprSeq *defaults,
                                         Ar_size_t arg_count,
                                         Ar_size_t default_count,
                                         ArStmtNode *body);
 
-AR_API_FUNC(ArExprNode *) arana_ast_Assign(AR_SIG, ArExprNode *target,
+AR_API_FUNC(ArExprNode *) arana_ast_Assign(AR_NODE_SIG, ArExprNode *target,
                                            ArExprNode *expr);
-AR_API_FUNC(ArExprNode *) arana_ast_AugAssign(AR_SIG, ArExprNode *target,
+AR_API_FUNC(ArExprNode *) arana_ast_AugAssign(AR_NODE_SIG, ArExprNode *target,
                                               ArExprNode *expr, ArAstOp op);
-AR_API_FUNC(ArExprNode *) arana_ast_BinaryOp(AR_SIG, ArExprNode *left,
+AR_API_FUNC(ArExprNode *) arana_ast_BinaryOp(AR_NODE_SIG, ArExprNode *left,
                                              ArExprNode *right, ArAstOp op);
-AR_API_FUNC(ArExprNode *) arana_ast_UnaryOp(AR_SIG, ArExprNode *expr, ArAstOp op);
-AR_API_FUNC(ArExprNode *) arana_ast_Call(AR_SIG, ArExprNode *expr,
+AR_API_FUNC(ArExprNode *) arana_ast_UnaryOp(AR_NODE_SIG, ArExprNode *expr, ArAstOp op);
+AR_API_FUNC(ArExprNode *) arana_ast_Call(AR_NODE_SIG, ArExprNode *expr,
                                          ArExprNode *args);
-AR_API_FUNC(ArExprNode *) arana_ast_Attribute(AR_SIG, ArExprNode *expr,
+AR_API_FUNC(ArExprNode *) arana_ast_Attribute(AR_NODE_SIG, ArExprNode *expr,
                                               ArExprNode *attr, ArAstCtx ctx);
-AR_API_FUNC(ArExprNode *) arana_ast_Subscript(AR_SIG, ArExprNode *expr,
+AR_API_FUNC(ArExprNode *) arana_ast_Subscript(AR_NODE_SIG, ArExprNode *expr,
                                               ArExprNode *index, ArAstCtx ctx);
-AR_API_FUNC(ArExprNode *) arana_ast_List(AR_SIG, ArExprSeq *items);
-AR_API_FUNC(ArExprNode *) arana_ast_Tuple(AR_SIG, ArExprSeq *items, ArAstCtx ctx);
-AR_API_FUNC(ArExprNode *) arana_ast_Interpolation(AR_SIG, ArExprSeq *items);
-AR_API_FUNC(ArExprNode *) arana_ast_Name(AR_SIG, const char *ident, ArAstCtx ctx);
-AR_API_FUNC(ArExprNode *) arana_ast_Literal(AR_SIG, AR value);
+AR_API_FUNC(ArExprNode *) arana_ast_List(AR_NODE_SIG, ArExprSeq *items);
+AR_API_FUNC(ArExprNode *) arana_ast_Tuple(AR_NODE_SIG, ArExprSeq *items, ArAstCtx ctx);
+AR_API_FUNC(ArExprNode *) arana_ast_Interpolation(AR_NODE_SIG, ArExprSeq *items);
+AR_API_FUNC(ArExprNode *) arana_ast_Name(AR_NODE_SIG, const char *ident, ArAstCtx ctx);
+AR_API_FUNC(ArExprNode *) arana_ast_Literal(AR_NODE_SIG, AR value);
 
 AR_API_FUNC(void) arana_ast_visit(AR_SIG, ArAstNode *node, ArAstVisitFunc pre,
                                   ArAstVisitFunc post, void *closure);
 AR_API_FUNC(void) arana_ast_set_store_ctx(AR_SIG, ArExprNode *node);
-AR_API_FUNC(void) arana_ast_set_filename(AR_SIG, ArAstNode *node,
-                                         const char *filename);
 AR_API_FUNC(void) arana_ast_optimize(AR_SIG, ArAstNode *node);
 AR_API_FUNC(void) arana_ast_dump(AR_SIG, ArAstNode *node, FILE *fp);
 
 }
 
 ArCode *
-arana_compile(AR_SIG, ArAstNode *node)
+arana_compile(AR_SIG, ArAstNode *node, const char *filename)
 {
     ArCompiler compiler;
-    compiler.unit = arana_new_code_unit(AR_ISIG, &compiler, node->filename);
+    compiler.unit = arana_new_code_unit(AR_ISIG, &compiler, filename);
     arana_compiler_visit(AR_ISIG, &compiler, node);
     arana_compiler_add_op(AR_ISIG, &compiler, AR_OP_STOP, 0);
     return arana_compiler_make_code(AR_ISIG, &compiler);
 #ifndef _ARANA_COMPILE_H
 #define _ARANA_COMPILE_H
 
-#define AR_COMPILE(n) arana_compile(AR_ISIG, n)
+#define AR_COMPILE(n, f) arana_compile(AR_ISIG, n, f)
 
 typedef struct _ArCode {
     const char *filename;
     ArCodeUnit *unit;
 } ArCompiler;
 
-AR_API_FUNC(ArCode *) arana_compile(AR_SIG, ArAstNode *node);
+AR_API_FUNC(ArCode *) arana_compile(AR_SIG, ArAstNode *node, const char *filename);
 
 #endif /* _ARANA_COMPILE_H */
 /* shortcuts for error creation */
 #define AR_ERROR(msg) arana_error_new(AR_ISIG, AR_STRING(msg))
 #define AR_INTERNAL_ERROR(msg) arana_internal_error_new(AR_ISIG, AR_STRING(msg))
-#define AR_SYNTAX_ERROR(msg, filename, lineno, column) arana_syntax_error_new(AR_ISIG, AR_STRING(msg), filename, lineno, column)
+#define AR_SYNTAX_ERROR(msg, filename, lineno, column) arana_syntax_error_new(AR_ISIG, AR_STRING(msg), AR_STRING(filename), lineno, column)
 #define AR_TYPE_ERROR(msg) arana_type_error_new(AR_ISIG, AR_STRING(msg))
 #define AR_ARGUMENT_ERROR(arg, pos, msg) arana_argument_error_new(AR_ISIG, arg, pos, AR_STRING(msg))
 #define AR_MEMORY_ERROR(msg) arana_memory_error_new(AR_ISIG, AR_STRING(msg))
 /* redefining AR_ISIG so that AR_ macros work in parser functions */
 #undef  AR_ISIG
 #define AR_ISIG  ps->AI, ps->CF
+
+/* easy setters for nodes' lineno, col and filename attributes */
+#define AR_AST2(ln, type, ...) \
+	arana_ast_##type(AR_ISIG, (ln)->lineno, (ln)->col, __VA_ARGS__)
+
+#define AR_AST_MAYBE_TUPLE(target, seq, ctx) {                          \
+        if (seq->count == 1) {                                          \
+            target = seq->items[0];                                     \
+        } else {                                                        \
+            target = AR_AST2(seq->items[0], Tuple, seq, ctx);           \
+        }                                                               \
+    }
 }
 
 %name ArParse
 %extra_argument { ArParseState *ps }
-%token_type     { AR }
+%token_type     { ArToken * }
 %token_prefix   AR_TOK_
 %token_destructor { ps = ps; }
 %syntax_error   {
 %type target       { ArExprNode * }
 %type targetlist   { ArExprSeq * }
 
-module ::= statements(B) newlines END.          { ps->ast = AR_AST(Module, B); }
+module ::= statements(B) newlines END.
+                                    { ps->ast = AR_AST2(B->items[0], Module, B); }
 
 newlines ::= NEWLINE newlines.
 newlines ::= .
 
-statements(A) ::= statements(B) statement(C). { AR_AST_SEQ_ADD(Stmt, A, B, C); }
-statements(A) ::= statement(B).                 { AR_AST_SEQ_NEW1(Stmt, A, B); }
+statements(A) ::= statements(B) statement(C).   { AR_AST_SEQ_ADD(Stmt, A, B, C); }
+statements(A) ::= statement(B).                   { AR_AST_SEQ_NEW1(Stmt, A, B); }
 
-statement(A) ::= IF expr(B) suite(C) ELSE suite(D). { A = AR_AST(If, B, C, D); }
-statement(A) ::= IF expr(B) suite(C).            { A = AR_AST(If, B, C, NULL); }
-statement(A) ::= FOR target(B) IN expr(C) suite(D).
-						   { A = AR_AST(For, B, C, D); }
-//statement(A) ::= DEF NAME(B) LPAREN arglist(C) RPAREN suite(D).
-//                               { A = AR_AST(def_stmt, AR_AS_CHARP(B), C, D); }
-statement(A) ::= small_stmts(B).                    { A = AR_AST(Compound, B); }
+statement(A) ::= IF(L) expr(B) suite(C) ELSE suite(D).
+                                                  { A = AR_AST2(L, If, B, C, D); }
+statement(A) ::= IF(L) expr(B) suite(C).       { A = AR_AST2(L, If, B, C, NULL); }
+statement(A) ::= FOR(L) target(B) IN expr(C) suite(D).
+                                                 { A = AR_AST2(L, For, B, C, D); }
+//statement(A) ::= DEF(L) NAME(B) LPAREN arglist(C) RPAREN suite(D).
+//                      { A = AR_AST2(L, def_stmt, AR_AS_CHARP(B->value), C, D); }
+statement(A) ::= small_stmts(B).        { A = AR_AST2(B->items[0], Compound, B); }
 
-suite(A) ::= COLON small_stmts(B).                  { A = AR_AST(Compound, B); }
-suite(A) ::= COLON NEWLINE INDENT statements(B) DEDENT.
-                                                    { A = AR_AST(Compound, B); }
+suite(A) ::= COLON(L) small_stmts(B).             { A = AR_AST2(L, Compound, B); }
+suite(A) ::= COLON NEWLINE INDENT(L) statements(B) DEDENT.
+                                                  { A = AR_AST2(L, Compound, B); }
 
-small_stmts(A) ::= small_stmt(B) NEWLINE.       { AR_AST_SEQ_NEW1(Stmt, A, B); }
+small_stmts(A) ::= small_stmt(B) NEWLINE.         { AR_AST_SEQ_NEW1(Stmt, A, B); }
 small_stmts(A) ::= small_stmt(B) SEMICOLON NEWLINE.
-                                                { AR_AST_SEQ_NEW1(Stmt, A, B); }
+                                                  { AR_AST_SEQ_NEW1(Stmt, A, B); }
 small_stmts(A) ::= small_stmt(B) SEMICOLON small_stmts(C).
-                                              { AR_AST_SEQ_ADD(Stmt, A, C, B); }
+                                                { AR_AST_SEQ_ADD(Stmt, A, C, B); }
 
-small_stmt(A) ::= PASS.                          { A = AR_AST(Compound, NULL); }
+small_stmt(A) ::= PASS(L).                     { A = AR_AST2(L, Compound, NULL); }
 small_stmt(A) ::= exprlist(B).
-                   { ArExprNode *tmp; AR_AST_MAYBE_TUPLE(tmp, B, AR_CTX_Load);
-                                                    A = AR_AST(ExprStmt, tmp); }
+                     { ArExprNode *tmp; AR_AST_MAYBE_TUPLE(tmp, B, AR_CTX_Load);
+                                                A = AR_AST2(tmp, ExprStmt, tmp); }
 small_stmt(A) ::= exprlist(B) COMMA.
-                        { A = AR_AST(ExprStmt, AR_AST(Tuple, B, AR_CTX_Load)); }
+                { ArExprNode *tmp = AR_AST2(B->items[0], Tuple, B, AR_CTX_Load);
+                                                A = AR_AST2(tmp, ExprStmt, tmp); }
 
 // simple expressions (literals and names)
-expr(A) ::= CONST(B).                                { A = AR_AST(Literal, B); }
-expr(A) ::= INTERP_START INTERP_END.     { A = AR_AST(Literal, AR_STRING("")); }
-expr(A) ::= INTERP_START interp_items(B) INTERP_END.
-                                               { A = AR_AST(Interpolation, B); }
-expr(A) ::= NAME(B).          { A = AR_AST(Name, AR_AS_CHARP(B), AR_CTX_Load); }
+expr(A) ::= CONST(B).                       { A = AR_AST2(B, Literal, B->value); }
+expr(A) ::= INTERP_START(L) INTERP_END.
+                                       { A = AR_AST2(L, Literal, AR_STRING("")); }
+expr(A) ::= INTERP_START(L) interp_items(B) INTERP_END.
+                                             { A = AR_AST2(L, Interpolation, B); }
+expr(A) ::= NAME(B). { A = AR_AST2(B, Name, AR_AS_CHARP(B->value), AR_CTX_Load); }
 
 // unary ops
-expr(A) ::= ADD expr(B). [NOT]         { A = AR_AST(UnaryOp, B, AR_ASTOP_POS); }
-expr(A) ::= SUB expr(B). [NOT]         { A = AR_AST(UnaryOp, B, AR_ASTOP_NEG); }
-expr(A) ::= NOT expr(B).               { A = AR_AST(UnaryOp, B, AR_ASTOP_NOT); }
+expr(A) ::= ADD(L) expr(B). [NOT]    { A = AR_AST2(L, UnaryOp, B, AR_ASTOP_POS); }
+expr(A) ::= SUB(L) expr(B). [NOT]    { A = AR_AST2(L, UnaryOp, B, AR_ASTOP_NEG); }
+expr(A) ::= NOT(L) expr(B).          { A = AR_AST2(L, UnaryOp, B, AR_ASTOP_NOT); }
 
 // binary ops
 expr(A) ::= expr(B) TILDE expr(C).
-                                { A = AR_AST(BinaryOp, B, C, AR_ASTOP_CONCAT); }
-expr(A) ::= expr(B) ADD expr(C).   { A = AR_AST(BinaryOp, B, C, AR_ASTOP_ADD); }
-expr(A) ::= expr(B) SUB expr(C).   { A = AR_AST(BinaryOp, B, C, AR_ASTOP_SUB); }
-expr(A) ::= expr(B) MUL expr(C).   { A = AR_AST(BinaryOp, B, C, AR_ASTOP_MUL); }
-expr(A) ::= expr(B) DIV expr(C).   { A = AR_AST(BinaryOp, B, C, AR_ASTOP_DIV); }
-expr(A) ::= expr(B) MOD expr(C).   { A = AR_AST(BinaryOp, B, C, AR_ASTOP_MOD); }
-expr(A) ::= expr(B) POW expr(C).   { A = AR_AST(BinaryOp, B, C, AR_ASTOP_POW); }
+                              { A = AR_AST2(B, BinaryOp, B, C, AR_ASTOP_CONCAT); }
+expr(A) ::= expr(B) ADD expr(C). { A = AR_AST2(B, BinaryOp, B, C, AR_ASTOP_ADD); }
+expr(A) ::= expr(B) SUB expr(C). { A = AR_AST2(B, BinaryOp, B, C, AR_ASTOP_SUB); }
+expr(A) ::= expr(B) MUL expr(C). { A = AR_AST2(B, BinaryOp, B, C, AR_ASTOP_MUL); }
+expr(A) ::= expr(B) DIV expr(C). { A = AR_AST2(B, BinaryOp, B, C, AR_ASTOP_DIV); }
+expr(A) ::= expr(B) MOD expr(C). { A = AR_AST2(B, BinaryOp, B, C, AR_ASTOP_MOD); }
+expr(A) ::= expr(B) POW expr(C). { A = AR_AST2(B, BinaryOp, B, C, AR_ASTOP_POW); }
 
-expr(A) ::= expr(B) EQ  expr(C).    { A = AR_AST(BinaryOp, B, C, AR_ASTOP_EQ); }
-expr(A) ::= expr(B) NEQ expr(C).   { A = AR_AST(BinaryOp, B, C, AR_ASTOP_NEQ); }
-expr(A) ::= expr(B) GT  expr(C).    { A = AR_AST(BinaryOp, B, C, AR_ASTOP_GT); }
-expr(A) ::= expr(B) GTE expr(C).   { A = AR_AST(BinaryOp, B, C, AR_ASTOP_GTE); }
-expr(A) ::= expr(B) LT  expr(C).    { A = AR_AST(BinaryOp, B, C, AR_ASTOP_LT); }
-expr(A) ::= expr(B) LTE expr(C).   { A = AR_AST(BinaryOp, B, C, AR_ASTOP_LTE); }
+expr(A) ::= expr(B) EQ  expr(C).  { A = AR_AST2(B, BinaryOp, B, C, AR_ASTOP_EQ); }
+expr(A) ::= expr(B) NEQ expr(C). { A = AR_AST2(B, BinaryOp, B, C, AR_ASTOP_NEQ); }
+expr(A) ::= expr(B) GT  expr(C).  { A = AR_AST2(B, BinaryOp, B, C, AR_ASTOP_GT); }
+expr(A) ::= expr(B) GTE expr(C). { A = AR_AST2(B, BinaryOp, B, C, AR_ASTOP_GTE); }
+expr(A) ::= expr(B) LT  expr(C).  { A = AR_AST2(B, BinaryOp, B, C, AR_ASTOP_LT); }
+expr(A) ::= expr(B) LTE expr(C). { A = AR_AST2(B, BinaryOp, B, C, AR_ASTOP_LTE); }
 
 // attribute access (XXX is that good?)
 expr(A) ::= expr(B) DOT NAME(C).
-                  { A = AR_AST(Attribute, B, AR_AST(Literal, C), AR_CTX_Load); }
+     { A = AR_AST2(B, Attribute, B, AR_AST2(C, Literal, C->value), AR_CTX_Load); }
 expr(A) ::= expr(B) DOT LBRACKET expr(C) RBRACKET.
-                                   { A = AR_AST(Attribute, B, C, AR_CTX_Load); }
+                                 { A = AR_AST2(B, Attribute, B, C, AR_CTX_Load); }
 
 // calls
-expr(A) ::= expr(B) LPAREN RPAREN.
-                      { A = AR_AST(Call, B, AR_AST(Tuple, NULL, AR_CTX_Load)); }
-expr(A) ::= expr(B) LPAREN exprlist(C) RPAREN.
-                         { A = AR_AST(Call, B, AR_AST(Tuple, C, AR_CTX_Load)); }
-expr(A) ::= expr(B) LPAREN exprlist(C) COMMA RPAREN.
-                         { A = AR_AST(Call, B, AR_AST(Tuple, C, AR_CTX_Load)); }
+expr(A) ::= expr(B) LPAREN(L) RPAREN.
+                { A = AR_AST2(B, Call, B, AR_AST2(L, Tuple, NULL, AR_CTX_Load)); }
+expr(A) ::= expr(B) LPAREN(L) exprlist(C) RPAREN.
+                   { A = AR_AST2(B, Call, B, AR_AST2(L, Tuple, C, AR_CTX_Load)); }
+expr(A) ::= expr(B) LPAREN(L) exprlist(C) COMMA RPAREN.
+                   { A = AR_AST2(B, Call, B, AR_AST2(L, Tuple, C, AR_CTX_Load)); }
 
 // tuple displays (or grouping)
-expr(A) ::= LPAREN RPAREN.             { A = AR_AST(Tuple, NULL, AR_CTX_Load); }
+expr(A) ::= LPAREN(L) RPAREN.        { A = AR_AST2(L, Tuple, NULL, AR_CTX_Load); }
 expr(A) ::= LPAREN exprlist(B) RPAREN.
-                                      { AR_AST_MAYBE_TUPLE(A, B, AR_CTX_Load); }
+                                        { AR_AST_MAYBE_TUPLE(A, B, AR_CTX_Load); }
 expr(A) ::= LPAREN exprlist(B) COMMA RPAREN.
-                                          { A = AR_AST(Tuple, B, AR_CTX_Load); }
+                              { A = AR_AST2(B->items[0], Tuple, B, AR_CTX_Load); }
 
 // list displays
-expr(A) ::= LBRACKET RBRACKET.                       { A = AR_AST(List, NULL); }
-expr(A) ::= LBRACKET exprlist(B) RBRACKET.              { A = AR_AST(List, B); }
-expr(A) ::= LBRACKET exprlist(B) COMMA RBRACKET.        { A = AR_AST(List, B); }
+expr(A) ::= LBRACKET(L) RBRACKET.                  { A = AR_AST2(L, List, NULL); }
+expr(A) ::= LBRACKET(L) exprlist(B) RBRACKET.         { A = AR_AST2(L, List, B); }
+expr(A) ::= LBRACKET(L) exprlist(B) COMMA RBRACKET.   { A = AR_AST2(L, List, B); }
 
 // subscription
 expr(A) ::= expr(B) LBRACKET expr(C) RBRACKET.
-                                   { A = AR_AST(Subscript, B, C, AR_CTX_Load); }
+                                 { A = AR_AST2(B, Subscript, B, C, AR_CTX_Load); }
 
 // assignments
-expr(A) ::= target(B) ASSIGN expr(C).              { A = AR_AST(Assign, B, C); }
+expr(A) ::= target(B) ASSIGN expr(C).            { A = AR_AST2(B, Assign, B, C); }
 expr(A) ::= simpletarget(B) ADDASSIGN expr(C).
-                                  { A = AR_AST(AugAssign, B, C, AR_ASTOP_ADD); }
+                                { A = AR_AST2(B, AugAssign, B, C, AR_ASTOP_ADD); }
 expr(A) ::= simpletarget(B) SUBASSIGN expr(C).
-                                  { A = AR_AST(AugAssign, B, C, AR_ASTOP_SUB); }
+                                { A = AR_AST2(B, AugAssign, B, C, AR_ASTOP_SUB); }
 expr(A) ::= simpletarget(B) MULASSIGN expr(C).
-                                  { A = AR_AST(AugAssign, B, C, AR_ASTOP_MUL); }
+                                { A = AR_AST2(B, AugAssign, B, C, AR_ASTOP_MUL); }
 expr(A) ::= simpletarget(B) DIVASSIGN expr(C).
-                                  { A = AR_AST(AugAssign, B, C, AR_ASTOP_DIV); }
+                                { A = AR_AST2(B, AugAssign, B, C, AR_ASTOP_DIV); }
 expr(A) ::= simpletarget(B) MODASSIGN expr(C).
-                                  { A = AR_AST(AugAssign, B, C, AR_ASTOP_MOD); }
+                                { A = AR_AST2(B, AugAssign, B, C, AR_ASTOP_MOD); }
 expr(A) ::= simpletarget(B) POWASSIGN expr(C).
-                                  { A = AR_AST(AugAssign, B, C, AR_ASTOP_POW); }
+                                { A = AR_AST2(B, AugAssign, B, C, AR_ASTOP_POW); }
 
 interp_items(A) ::= interp_items(B) interp_item(C).
-                                              { AR_AST_SEQ_ADD(Expr, A, B, C); }
-interp_items(A) ::= interp_item(B).             { AR_AST_SEQ_NEW1(Expr, A, B); }
+                                                { AR_AST_SEQ_ADD(Expr, A, B, C); }
+interp_items(A) ::= interp_item(B).               { AR_AST_SEQ_NEW1(Expr, A, B); }
 
-interp_item(A) ::= INTERP_STRING(B).                 { A = AR_AST(Literal, B); }
-interp_item(A) ::= LBRACE expr(B) RBRACE.                             { A = B; }
+interp_item(A) ::= INTERP_STRING(B).        { A = AR_AST2(B, Literal, B->value); }
+interp_item(A) ::= LBRACE expr(B) RBRACE.                               { A = B; }
 
-exprlist(A) ::= exprlist(B) COMMA expr(C).    { AR_AST_SEQ_ADD(Expr, A, B, C); }
-exprlist(A) ::= expr(B).                        { AR_AST_SEQ_NEW1(Expr, A, B); }
+exprlist(A) ::= exprlist(B) COMMA expr(C).      { AR_AST_SEQ_ADD(Expr, A, B, C); }
+exprlist(A) ::= expr(B).                          { AR_AST_SEQ_NEW1(Expr, A, B); }
 
-simpletarget(A) ::= NAME(B). { A = AR_AST(Name, AR_AS_CHARP(B), AR_CTX_Store); }
+simpletarget(A) ::= NAME(B).
+                    { A = AR_AST2(B, Name, AR_AS_CHARP(B->value), AR_CTX_Store); }
 simpletarget(A) ::= expr(B) DOT NAME(C).
-                 { A = AR_AST(Attribute, B, AR_AST(Literal, C), AR_CTX_Store); }
+    { A = AR_AST2(B, Attribute, B, AR_AST2(C, Literal, C->value), AR_CTX_Store); }
 simpletarget(A) ::= expr(B) DOT LBRACKET expr(C) RBRACKET.
-                                  { A = AR_AST(Attribute, B, C, AR_CTX_Store); }
+                                { A = AR_AST2(B, Attribute, B, C, AR_CTX_Store); }
 simpletarget(A) ::= expr(B) LBRACKET expr(C) RBRACKET.
-                                  { A = AR_AST(Subscript, B, C, AR_CTX_Store); }
+                                { A = AR_AST2(B, Subscript, B, C, AR_CTX_Store); }
 
-target(A) ::= simpletarget(B).                                        { A = B; }
+target(A) ::= simpletarget(B).                                          { A = B; }
 target(A) ::= LPAREN exprlist(B) RPAREN.
-            { AR_AST_MAYBE_TUPLE(A, B, AR_CTX_Store); AR_AST_SET_STORE_CTX(A); }
+              { AR_AST_MAYBE_TUPLE(A, B, AR_CTX_Store); AR_AST_SET_STORE_CTX(A); }
 target(A) ::= LPAREN exprlist(B) COMMA RPAREN.
-                { A = AR_AST(Tuple, B, AR_CTX_Store); AR_AST_SET_STORE_CTX(A); }
+    { A = AR_AST2(B->items[0], Tuple, B, AR_CTX_Store); AR_AST_SET_STORE_CTX(A); }

arana/lexnparse.h

 
 #define AR_PARSE_ERROR(ps) arana_parse_error(AR_ISIG, ps)
 
+typedef struct _ArToken {
+    AR value;
+    int lineno;
+    int col;
+} ArToken;
 
 typedef struct _ArParseState {
     struct _ArInterpreter *AI;
     struct _ArFrame *CF;
     ArToplevelNode *ast;         // contains the parse result
-    AR filename;                 // source filename
-    Ar_size_t lineno;            // current line number
+    const char *filename;        // source filename
+    int lineno;                  // current line number
     int lasttok;                 // type of last lexed token
     char *tokstart;              // start of last lexed token
     char *tokend;                // end of last lexed token
 void *ArParseAlloc(void *(*)(size_t));
 void ArParseFree(void *, void (*)(void *));
 void ArParseTrace(FILE *, char *);
-int ArParse(void *, int, AR, ArParseState *, int);
+int ArParse(void *, int, ArToken *, ArParseState *, int);
 
 /* arana parser APIs */
 

arana/lexnparse.rl

 #include "arana.h"
 
 #define TOKEN(id)     TOKEN2(id, AR_NONE)
-#define TOKEN2(id, v) ps->tokstart = ts; ps->tokend = te; ps->lasttok = AR_TOK_##id; \
-    /* printf(#id " %s\n", AR_IS_STRING(v) ? AR_AS_CHARP(v) : ""); */ \
-    ArParse(pParser, AR_TOK_##id, v, ps, 0)
+#define TOKEN2(id, v)                                                   \
+    ps->tokstart = ts; ps->tokend = te; ps->lasttok = AR_TOK_##id;      \
+    /* printf(#id " %s\n", AR_IS_STRING(v) ? AR_AS_CHARP(v) : ""); */   \
+    tok = AR_ALLOCT(ArToken);                                           \
+    if (!tok) AR_RAISE_OOM();                                           \
+    tok->value = v;                                                     \
+    tok->lineno = ps->lineno;                                           \
+    tok->col = ps->tokstart - ps->lineoffset;                           \
+    ArParse(pParser, AR_TOK_##id, tok, ps, 0)
 #define ISLAST(id)    ps->lasttok == AR_TOK_##id
 
 #define STATESTACKSIZE  100
         AR_FATAL("arana_parse_error: unhandled errortype value\n");
     }
     ps->AI->error = AR_SYNTAX_ERROR(buf, ps->filename, ps->lineno, offset);
-    arana_traceback_push(AR_ISIG, AR_AS_CHARP(ps->filename), ps->lineno, NULL);
+    arana_traceback_push(AR_ISIG, ps->filename, ps->lineno, NULL);
     arana_error_jump(AR_ISIG);
 }
 
     char *buffer;
     int bufsize, space, prefixlen = 0;
     int done, line = 0;
+    ArToken *tok = NULL;  // current token
 
     if (fp) {
         bufsize = 1024;
     ps->AI = AI;
     ps->CF = CF;
     ps->ast = NULL;
-    ps->filename = filename;
+    ps->filename = AR_AS_CHARP(filename);
     ps->lineno = 1;
     ps->lasttok = -1;
     ps->nesting = 0;
         ast = (ArAstNode *)arana_parse_file(AR_ISIG, f, AR_STRING(fn));
     }
     /* execute code -- XXX factor that out */
-    arana_ast_set_filename(AR_ISIG, ast, fn);
     arana_ast_optimize(AR_ISIG, ast);
     arana_ast_dump(AR_ISIG, ast, stdout);
-    ArCode *codeobj = AR_COMPILE(ast);
+    ArCode *codeobj = AR_COMPILE(ast, fn);
     AR_DEBUG_CODE(codeobj);
     ArFrame *frame = AR_FRAME(codeobj);
     AR_EVALUATE(frame);
         return AR_NULL;
     }
 
-    arana_ast_set_filename(AR_ISIG, ast, "<stdin>");
     arana_ast_optimize(AR_ISIG, ast);
     arana_ast_dump(AR_ISIG, ast, stdout);
-    ArCode *codeobj = AR_COMPILE(ast);
+    ArCode *codeobj = AR_COMPILE(ast, "<stdin>");
     AR_DEBUG_CODE(codeobj);
     ArFrame *frame = AR_FRAME(codeobj);
     AR_EVALUATE(frame);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.