From e311dbeb60c13539a9f6175956a18c6e1e84ae0b Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=BCrg=20Billeter?= Date: Mon, 24 Apr 2006 14:00:03 +0000 Subject: [PATCH] support single-line comments, ASSIGN, PLUS, RETURN, LITERAL_INTEGER, MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 2006-04-24 Jürg Billeter * valac/scanner.l: support single-line comments, ASSIGN, PLUS, RETURN, LITERAL_INTEGER, LITERAL_STRING * valac/parser.y: add basic statements and expressions * valac/context.h: add ValaStatement, ValaVariableDeclaration, ValaVariableDeclarator, and ValaExpression structs * valac/context.c: remove unreachable line svn path=/trunk/; revision=4 --- vala/ChangeLog | 9 ++ vala/valac/context.c | 1 - vala/valac/context.h | 79 ++++++++++++ vala/valac/parser.y | 335 ++++++++++++++++++++++++++++++++++++++++++++++++++- vala/valac/scanner.l | 11 +- 5 files changed, 429 insertions(+), 6 deletions(-) diff --git a/vala/ChangeLog b/vala/ChangeLog index 019cd38..ee8e8ff 100644 --- a/vala/ChangeLog +++ b/vala/ChangeLog @@ -1,5 +1,14 @@ 2006-04-24 Jürg Billeter + * valac/scanner.l: support single-line comments, ASSIGN, PLUS, RETURN, + LITERAL_INTEGER, LITERAL_STRING + * valac/parser.y: add basic statements and expressions + * valac/context.h: add ValaStatement, ValaVariableDeclaration, + ValaVariableDeclarator, and ValaExpression structs + * valac/context.c: remove unreachable line + +2006-04-24 Jürg Billeter + * valac/scanner.l: set token location, support OPEN_PARENS, CLOSE_PARENS, SEMICOLON, PUBLIC, STATIC * valac/parser.y: save symbol location, output exact error location, diff --git a/vala/valac/context.c b/vala/valac/context.c index baa534d..c39a28b 100644 --- a/vala/valac/context.c +++ b/vala/valac/context.c @@ -176,7 +176,6 @@ vala_context_resolve_type_reference (ValaContext *context, ValaNamespace *namesp } else { /* specified namespace not found */ err (type_reference->location, "error: specified type ´%s´ not found", type_reference->type_name); - exit (1); } } else { /* namespace has been explicitly specified */ diff --git a/vala/valac/context.h b/vala/valac/context.h index b1c3ecb..d81a493 100644 --- a/vala/valac/context.h +++ b/vala/valac/context.h @@ -24,6 +24,9 @@ typedef enum _ValaSymbolType ValaSymbolType; typedef enum _ValaMethodFlags ValaMethodFlags; +typedef enum _ValaStatementType ValaStatementType; +typedef enum _ValaExpressionType ValaExpressionType; +typedef enum _ValaOpType ValaOpType; typedef struct _ValaContext ValaContext; typedef struct _ValaSymbol ValaSymbol; @@ -32,6 +35,10 @@ typedef struct _ValaLocation ValaLocation; typedef struct _ValaNamespace ValaNamespace; typedef struct _ValaClass ValaClass; typedef struct _ValaMethod ValaMethod; +typedef struct _ValaStatement ValaStatement; +typedef struct _ValaVariableDeclaration ValaVariableDeclaration; +typedef struct _ValaVariableDeclarator ValaVariableDeclarator; +typedef struct _ValaExpression ValaExpression; typedef struct _ValaTypeReference ValaTypeReference; typedef struct _ValaFormalParameter ValaFormalParameter; @@ -47,6 +54,30 @@ enum _ValaMethodFlags { VALA_METHOD_STATIC = 0x02, }; +enum _ValaStatementType { + VALA_STATEMENT_TYPE_BLOCK, + VALA_STATEMENT_TYPE_EXPRESSION, + VALA_STATEMENT_TYPE_RETURN, + VALA_STATEMENT_TYPE_VARIABLE_DECLARATION, +}; + +enum _ValaExpressionType { + VALA_EXPRESSION_TYPE_ADDITIVE, + VALA_EXPRESSION_TYPE_ASSIGNMENT, + VALA_EXPRESSION_TYPE_EXPRESSION, + VALA_EXPRESSION_TYPE_INVOCATION, + VALA_EXPRESSION_TYPE_LITERAL_INTEGER, + VALA_EXPRESSION_TYPE_LITERAL_STRING, + VALA_EXPRESSION_TYPE_MEMBER_ACCESS, + VALA_EXPRESSION_TYPE_PARENTHESIZED, + VALA_EXPRESSION_TYPE_RETURN, + VALA_EXPRESSION_TYPE_SIMPLE_NAME, +}; + +enum _ValaOpType { + VALA_OP_TYPE_PLUS, +}; + struct _ValaContext { GList *source_files; ValaSymbol *root; @@ -100,6 +131,54 @@ struct _ValaMethod { ValaMethodFlags modifiers; char *cdecl1; char *cdecl2; + ValaStatement *body; +}; + +struct _ValaStatement { + ValaStatementType type; + union { + struct { + GList *statements; + } block; + ValaExpression *expr; + ValaVariableDeclaration *variable_declaration; + }; +}; + +struct _ValaVariableDeclaration { + ValaTypeReference *type; + ValaVariableDeclarator *declarator; +}; + +struct _ValaVariableDeclarator { + char *name; + ValaLocation *location; + ValaExpression *initializer; +}; + +struct _ValaExpression { + ValaExpressionType type; + union { + char *str; + ValaExpression *inner; + struct { + ValaExpression *left; + ValaOpType op; + ValaExpression *right; + } additive; + struct { + ValaExpression *left; + char *right; + } member_access; + struct { + ValaExpression *call; + GList *argument_list; + } invocation; + struct { + ValaExpression *left; + ValaExpression *right; + } assignment; + }; }; struct _ValaTypeReference { diff --git a/vala/valac/parser.y b/vala/valac/parser.y index 7b36ec5..f356d0a 100644 --- a/vala/valac/parser.y +++ b/vala/valac/parser.y @@ -115,6 +115,10 @@ ValaLocation *get_location (int lineno, int colno) GList *list; ValaTypeReference *type_reference; ValaFormalParameter *formal_parameter; + ValaStatement *statement; + ValaVariableDeclaration *variable_declaration; + ValaVariableDeclarator *variable_declarator; + ValaExpression *expression; } %token OPEN_BRACE "{" @@ -125,24 +129,56 @@ ValaLocation *get_location (int lineno, int colno) %token COLON ":" %token COMMA "," %token SEMICOLON ";" +%token ASSIGN "=" +%token PLUS "+" -%token NAMESPACE "namespace" %token CLASS "class" +%token NAMESPACE "namespace" %token PUBLIC "public" +%token RETURN "return" %token STATIC "static" + %token IDENTIFIER "identifier" +%token LITERAL_INTEGER "integer" +%token LITERAL_STRING "string" %type opt_class_base %type class_base %type type_list -%type type_name %type opt_formal_parameter_list %type formal_parameter_list %type fixed_parameters -%type fixed_parameter +%type opt_statement_list +%type statement_list +%type opt_argument_list +%type argument_list %type opt_method_modifiers %type method_modifiers %type method_modifier +%type type_name +%type variable_initializer +%type opt_expression +%type expression +%type additive_expression +%type assignment +%type primary_expression +%type literal +%type simple_name +%type parenthesized_expression +%type member_access +%type invocation_expression +%type statement_expression +%type argument +%type block +%type statement +%type declaration_statement +%type embedded_statement +%type expression_statement +%type jump_statement +%type return_statement +%type fixed_parameter +%type variable_declaration +%type variable_declarator %% @@ -374,11 +410,302 @@ fixed_parameter method_body : block + { + current_method->body = $1; + } | SEMICOLON ; block - : OPEN_BRACE CLOSE_BRACE + : OPEN_BRACE opt_statement_list CLOSE_BRACE + { + $$ = g_new0 (ValaStatement, 1); + $$->type = VALA_STATEMENT_TYPE_BLOCK; + $$->block.statements = $2; + } + ; + +opt_statement_list + : /* empty */ + { + $$ = NULL; + } + | statement_list + { + $$ = $1; + } + ; + +statement_list + : statement + { + $$ = g_list_append (NULL, $1); + } + | statement_list statement + { + $$ = g_list_append ($1, $2); + } + ; + +statement + : declaration_statement + { + $$ = $1; + } + | embedded_statement + { + $$ = $1; + } + ; + +declaration_statement + : variable_declaration SEMICOLON + { + $$ = g_new0 (ValaStatement, 1); + $$->type = VALA_STATEMENT_TYPE_VARIABLE_DECLARATION; + $$->variable_declaration = $1;; + } + ; + +variable_declaration + : type_name variable_declarator + { + $$ = g_new0 (ValaVariableDeclaration, 1); + $$->type = $1; + $$->declarator = $2; + } + ; + +variable_declarator + : IDENTIFIER + { + $$ = g_new0 (ValaVariableDeclarator, 1); + $$->name = $1; + } + | IDENTIFIER ASSIGN variable_initializer + { + $$ = g_new0 (ValaVariableDeclarator, 1); + $$->name = $1; + $$->initializer = $3; + } + ; + +variable_initializer + : expression + { + $$ = $1; + } + ; + +opt_expression + : /* empty */ + { + $$ = NULL; + } + | expression + { + $$ = $1; + } + ; + +expression + : additive_expression + { + $$ = $1; + } + | assignment + { + $$ = $1; + } + ; + +additive_expression + : primary_expression + { + $$ = $1; + } + | additive_expression PLUS primary_expression + { + $$ = g_new0 (ValaExpression, 1); + $$->type = VALA_EXPRESSION_TYPE_ADDITIVE; + $$->additive.left = $1; + $$->additive.op = VALA_OP_TYPE_PLUS; + $$->additive.right = $3; + } + ; + +primary_expression + : literal + { + $$ = $1; + } + | simple_name + { + $$ = $1; + } + | parenthesized_expression + { + $$ = $1; + } + | member_access + { + $$ = $1; + } + | invocation_expression + { + $$ = $1; + } + ; + +literal + : LITERAL_INTEGER + { + $$ = g_new0 (ValaExpression, 1); + $$->type = VALA_EXPRESSION_TYPE_LITERAL_INTEGER; + $$->str = $1; + } + | LITERAL_STRING + { + $$ = g_new0 (ValaExpression, 1); + $$->type = VALA_EXPRESSION_TYPE_LITERAL_STRING; + $$->str = $1; + } + ; + +simple_name + : IDENTIFIER + { + $$ = g_new0 (ValaExpression, 1); + $$->type = VALA_EXPRESSION_TYPE_SIMPLE_NAME; + $$->str = $1; + } + ; + +parenthesized_expression + : OPEN_PARENS expression CLOSE_PARENS + { + $$ = g_new0 (ValaExpression, 1); + $$->type = VALA_EXPRESSION_TYPE_PARENTHESIZED; + $$->inner = $2; + } + ; + +member_access + : primary_expression DOT IDENTIFIER + { + $$ = g_new0 (ValaExpression, 1); + $$->type = VALA_EXPRESSION_TYPE_MEMBER_ACCESS; + $$->member_access.left = $1; + $$->member_access.right = $3; + } + ; + +invocation_expression + : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS + { + $$ = g_new0 (ValaExpression, 1); + $$->type = VALA_EXPRESSION_TYPE_INVOCATION; + $$->invocation.call = $1; + $$->invocation.argument_list = $3; + } + ; + +opt_argument_list + : /* empty */ + { + $$ = NULL; + } + | argument_list + { + $$ = $1; + } + ; + +argument_list + : argument + { + $$ = g_list_append (NULL, $1); + } + | argument_list COMMA argument + { + $$ = g_list_append ($1, $3); + } + ; + +argument + : expression + { + $$ = $1; + } + ; + +embedded_statement + : block + { + $$ = $1; + } + | empty_statement + { + $$ = NULL; + } + | expression_statement + { + $$ = $1; + } + | jump_statement + { + $$ = $1; + } + ; + +empty_statement + : SEMICOLON + ; + +expression_statement + : statement_expression SEMICOLON + { + $$ = g_new0 (ValaStatement, 1); + $$->type = VALA_STATEMENT_TYPE_EXPRESSION; + $$->expr = $1; + } + ; + +statement_expression + : invocation_expression + { + $$ = $1; + } + | assignment + { + $$ = $1; + } + ; + +assignment + : primary_expression ASSIGN expression + { + $$ = g_new0 (ValaExpression, 1); + $$->type = VALA_EXPRESSION_TYPE_ASSIGNMENT; + $$->assignment.left = $1; + $$->assignment.right = $3; + } + ; + +jump_statement + : return_statement + { + $$ = $1; + } + ; + +return_statement + : RETURN opt_expression SEMICOLON + { + $$ = g_new0 (ValaStatement, 1); + $$->type = VALA_STATEMENT_TYPE_RETURN; + $$->expr = $2; + } ; %% diff --git a/vala/valac/scanner.l b/vala/valac/scanner.l index 0a41db5..64dc066 100644 --- a/vala/valac/scanner.l +++ b/vala/valac/scanner.l @@ -34,6 +34,8 @@ %% +"//".* { uploc; } + "{" { uploc; return OPEN_BRACE; } "}" { uploc; return CLOSE_BRACE; } "(" { uploc; return OPEN_PARENS; } @@ -42,12 +44,19 @@ ":" { uploc; return COLON; } "," { uploc; return COMMA; } ";" { uploc; return SEMICOLON; } +"=" { uploc; return ASSIGN; } +"+" { uploc; return PLUS; } -"namespace" { uploc; return NAMESPACE; } "class" { uploc; return CLASS; } +"namespace" { uploc; return NAMESPACE; } "public" { uploc; return PUBLIC; } +"return" { uploc; return RETURN; } "static" { uploc; return STATIC; } +[[:digit:]]+ { uploc; yylval->str = strdup (yytext); return LITERAL_INTEGER; } + +\"([^\"\\]|\\[\'\"\?\\abfnrtv])*\" { uploc; yylval->str = strdup (yytext); return LITERAL_STRING; } + [[:alnum:]_]+ { uploc; yylval->str = strdup (yytext); return IDENTIFIER; } [ \t]+ { uploc; /* eat up whitespace */ } -- 2.7.4