diff options
-rw-r--r-- | src/parser.l | 51 | ||||
-rw-r--r-- | src/parser.y | 49 |
2 files changed, 79 insertions, 21 deletions
diff --git a/src/parser.l b/src/parser.l index 59a0358..cbf359c 100644 --- a/src/parser.l +++ b/src/parser.l | |||
@@ -2,20 +2,28 @@ | |||
2 | #include <stdint.h> | 2 | #include <stdint.h> |
3 | #include <bu/string.h> | 3 | #include <bu/string.h> |
4 | #include "parser.tab.h" | 4 | #include "parser.tab.h" |
5 | |||
6 | //#define YY_DECL int yylex( YYSTYPE *yylval, struct YYLTYPE *llocp, yyscan_t yyscanner ) | ||
7 | |||
8 | //YY_DECL; | ||
9 | |||
10 | # define YY_USER_ACTION yylloc->last_column += yyleng; | ||
5 | %} | 11 | %} |
6 | 12 | ||
7 | %option noyywrap | 13 | %option reentrant noyywrap nounput batch |
14 | %option bison-bridge bison-locations | ||
8 | 15 | ||
9 | %x sitname | 16 | %x sitname |
10 | %x comment | 17 | %x comment |
11 | %x dqstr tdqstr tsqstr | 18 | %x dqstr tdqstr tsqstr |
12 | %% | 19 | %% |
13 | 20 | ||
14 | [-{}<>=+/*,();:] { return yytext[0]; } | 21 | [-{}<>=+/*,();:.] { return yytext[0]; } |
15 | 22 | ||
16 | game { return tokGame; } | 23 | game { return tokGame; } |
17 | function { return tokFunction; } | 24 | function { return tokFunction; } |
18 | situation { return tokSituation; } | 25 | situation { return tokSituation; } |
26 | player { return tokPlayer; } | ||
19 | while { return tokWhile; } | 27 | while { return tokWhile; } |
20 | for { return tokFor; } | 28 | for { return tokFor; } |
21 | each { return tokEach; } | 29 | each { return tokEach; } |
@@ -27,12 +35,12 @@ command { return tokCommand; } | |||
27 | goto { return tokGoto; } | 35 | goto { return tokGoto; } |
28 | not { return tokNot; } | 36 | not { return tokNot; } |
29 | 37 | ||
30 | true { yylval.bValue = true; return tokBool; } | 38 | true { yylval->bValue = true; return tokBool; } |
31 | false { yylval.bValue = false; return tokBool; } | 39 | false { yylval->bValue = false; return tokBool; } |
32 | null { return tokNull; } | 40 | null { return tokNull; } |
33 | 41 | ||
34 | "<<" { BEGIN( sitname ); } | 42 | "<<" { BEGIN( sitname ); } |
35 | <sitname>[- a-zA-Z0-9]+ { yylval.sValue = new Bu::String( yytext ); return tokSituationName; } | 43 | <sitname>[- a-zA-Z0-9]+ { yylval->sValue = new Bu::String( yytext ); return tokSituationName; } |
36 | <sitname>">>" { BEGIN( INITIAL ); } | 44 | <sitname>">>" { BEGIN( INITIAL ); } |
37 | <sitname>. REJECT; | 45 | <sitname>. REJECT; |
38 | 46 | ||
@@ -42,32 +50,41 @@ null { return tokNull; } | |||
42 | <comment>"\*/" { BEGIN( INITIAL ); } | 50 | <comment>"\*/" { BEGIN( INITIAL ); } |
43 | <comment>. {} | 51 | <comment>. {} |
44 | 52 | ||
45 | [a-zA-Z_][a-zA-Z0-9_]* { yylval.sValue = new Bu::String( yytext ); return tokIdent; } | 53 | [a-zA-Z_][a-zA-Z0-9_]* { yylval->sValue = new Bu::String( yytext ); return tokIdent; } |
46 | 54 | ||
47 | [1-9][0-9]* { | 55 | [1-9][0-9]* { |
48 | yylval.iValue = strtoll( yytext, NULL, 10 ); | 56 | yylval->iValue = strtoll( yytext, NULL, 10 ); |
49 | return tokInt; | 57 | return tokInt; |
50 | } | 58 | } |
51 | 59 | ||
52 | ([1-9][0-9]*)?\.[0-9]* { | 60 | ([1-9][0-9]*)?\.[0-9]* { |
53 | yylval.dValue = strtod( yytext, NULL ); | 61 | yylval->dValue = strtod( yytext, NULL ); |
54 | return tokFloat; | 62 | return tokFloat; |
55 | } | 63 | } |
56 | 64 | ||
57 | \"\"\" { BEGIN( tdqstr ); yylval.sValue = new Bu::String(); } | 65 | \"\"\" { BEGIN( tdqstr ); yylval->sValue = new Bu::String(); } |
58 | <tdqstr>[^"]+ { (*yylval.sValue) += yytext; } | 66 | <tdqstr>[^"]+ { (*yylval->sValue) += yytext; } |
59 | <tdqstr>\" { (*yylval.sValue) += yytext; } | 67 | <tdqstr>\" { (*yylval->sValue) += yytext; } |
60 | <tdqstr>\"\"\" { BEGIN( INITIAL ); return tokString; } | 68 | <tdqstr>\"\"\" { BEGIN( INITIAL ); return tokString; } |
61 | 69 | ||
62 | \'\'\' { BEGIN( tsqstr ); yylval.sValue = new Bu::String(); } | 70 | \'\'\' { BEGIN( tsqstr ); yylval->sValue = new Bu::String(); } |
63 | <tsqstr>[^']+ { (*yylval.sValue) += yytext; } | 71 | <tsqstr>[^']+ { (*yylval->sValue) += yytext; } |
64 | <tsqstr>\' { (*yylval.sValue) += yytext; } | 72 | <tsqstr>\' { (*yylval->sValue) += yytext; } |
65 | <tsqstr>\'\'\' { BEGIN( INITIAL ); return tokString; } | 73 | <tsqstr>\'\'\' { BEGIN( INITIAL ); return tokString; } |
66 | 74 | ||
67 | \" { BEGIN( dqstr ); yylval.sValue = new Bu::String(); } | 75 | \" { BEGIN( dqstr ); yylval->sValue = new Bu::String(); } |
68 | <dqstr>[^"]+ { (*yylval.sValue) += yytext; } | 76 | <dqstr>[^"]+ { (*yylval->sValue) += yytext; } |
69 | <dqstr>\" { BEGIN( INITIAL ); return tokString; } | 77 | <dqstr>\" { BEGIN( INITIAL ); return tokString; } |
70 | 78 | ||
71 | [ \t\n\r]+ {} | 79 | [ \t]+ { |
80 | yylloc->first_line = yylloc->last_line; | ||
81 | yylloc->first_column = yylloc->last_column+1; | ||
82 | } | ||
83 | |||
84 | \n+ { | ||
85 | yylloc->last_column = yylloc->first_column = 0; | ||
86 | yylloc->last_line+= yyleng; | ||
87 | yylloc->first_line = yylloc->last_line; | ||
88 | } | ||
72 | 89 | ||
73 | %% | 90 | %% |
diff --git a/src/parser.y b/src/parser.y index 7242df0..e2f8b6d 100644 --- a/src/parser.y +++ b/src/parser.y | |||
@@ -3,10 +3,19 @@ | |||
3 | #include <stdio.h> | 3 | #include <stdio.h> |
4 | #include <bu/string.h> | 4 | #include <bu/string.h> |
5 | 5 | ||
6 | int yylex(); | 6 | typedef void *yyscan_t; |
7 | void yyerror( const char *error ) { printf("%s\n", error ); } | 7 | |
8 | %} | 8 | %} |
9 | 9 | ||
10 | |||
11 | %locations | ||
12 | %define api.pure | ||
13 | %debug | ||
14 | %error-verbose | ||
15 | |||
16 | %parse-param { yyscan_t yyscanner } | ||
17 | %lex-param { yysacn_t yyscanner } | ||
18 | |||
10 | %union { | 19 | %union { |
11 | int64_t iValue; | 20 | int64_t iValue; |
12 | double dValue; | 21 | double dValue; |
@@ -14,6 +23,17 @@ void yyerror( const char *error ) { printf("%s\n", error ); } | |||
14 | bool bValue; | 23 | bool bValue; |
15 | } | 24 | } |
16 | 25 | ||
26 | %{ | ||
27 | int yylex( YYSTYPE *yylval, struct YYLTYPE *llocp, yyscan_t yyscanner ); | ||
28 | void yyerror( YYLTYPE *llocp, yyscan_t yyscanner, const char *error ) | ||
29 | { | ||
30 | printf("%d:%d-%d:%d: %s\n", | ||
31 | llocp->first_line, llocp->first_column, | ||
32 | llocp->last_line, llocp->last_column, error | ||
33 | ); | ||
34 | } | ||
35 | %} | ||
36 | |||
17 | %token tokGame | 37 | %token tokGame |
18 | %token tokFunction | 38 | %token tokFunction |
19 | %token tokSituation | 39 | %token tokSituation |
@@ -26,6 +46,7 @@ void yyerror( const char *error ) { printf("%s\n", error ); } | |||
26 | %token tokElse | 46 | %token tokElse |
27 | %token tokNot | 47 | %token tokNot |
28 | %token tokCommand | 48 | %token tokCommand |
49 | %token tokPlayer | ||
29 | %token <sValue> tokSituationName | 50 | %token <sValue> tokSituationName |
30 | %token <sValue> tokIdent | 51 | %token <sValue> tokIdent |
31 | %token tokGoto | 52 | %token tokGoto |
@@ -39,6 +60,8 @@ void yyerror( const char *error ) { printf("%s\n", error ); } | |||
39 | 60 | ||
40 | %token eos 0 "end of stream" | 61 | %token eos 0 "end of stream" |
41 | 62 | ||
63 | %right '=' | ||
64 | %left tokIn | ||
42 | %left '(' ')' '[' ']' | 65 | %left '(' ')' '[' ']' |
43 | %left '*' '/' | 66 | %left '*' '/' |
44 | %left '-' '+' | 67 | %left '-' '+' |
@@ -78,24 +101,35 @@ cmpltExprList: | |||
78 | 101 | ||
79 | cmpltExpr: expr ';' | 102 | cmpltExpr: expr ';' |
80 | | tokGoto '(' expr ')' ';' | 103 | | tokGoto '(' expr ')' ';' |
104 | | tokIf expr tokThen '{' cmpltExprList '}' ifnext | ||
81 | ; | 105 | ; |
82 | 106 | ||
107 | ifnext: | ||
108 | | tokElse '{' cmpltExprList '}' | ||
109 | | tokElse tokIf '{' cmpltExprList '}' ifnext | ||
110 | ; | ||
111 | |||
83 | expr: tokInt | 112 | expr: tokInt |
84 | | tokFloat | 113 | | tokFloat |
85 | | tokString | 114 | | tokString |
86 | | tokBool | 115 | | tokBool |
87 | | tokNull | 116 | | tokNull |
117 | | tokIdent | ||
88 | | tokSituationName | 118 | | tokSituationName |
119 | | tokSituation '.' tokIdent | ||
120 | | tokPlayer '.' tokIdent | ||
89 | | expr '+' expr | 121 | | expr '+' expr |
90 | | expr '-' expr | 122 | | expr '-' expr |
91 | | expr '/' expr | 123 | | expr '/' expr |
92 | | expr '*' expr | 124 | | expr '*' expr |
125 | | expr tokIn expr | ||
93 | | '(' expr ')' | 126 | | '(' expr ')' |
94 | | expr '[' expr ']' | 127 | | expr '[' expr ']' |
95 | | '[' ']' | 128 | | '[' ']' |
96 | | '[' listValues ']' | 129 | | '[' listValues ']' |
97 | | '{' '}' | 130 | | '{' '}' |
98 | | '{' dictValues '}' | 131 | | '{' dictValues '}' |
132 | /* | tokNot expr */ | ||
99 | ; | 133 | ; |
100 | 134 | ||
101 | listValues: expr | 135 | listValues: expr |
@@ -106,7 +140,7 @@ dictValues: expr ':' expr | |||
106 | | dictValues ',' expr ':' expr | 140 | | dictValues ',' expr ':' expr |
107 | ; | 141 | ; |
108 | 142 | ||
109 | commandDecl: tokCommand ':' tokString commandParamList '{' '}' | 143 | commandDecl: tokCommand ':' tokString commandParamList '{' cmpltExprList '}' |
110 | ; | 144 | ; |
111 | 145 | ||
112 | commandParamList: | 146 | commandParamList: |
@@ -115,9 +149,16 @@ commandParamList: | |||
115 | ; | 149 | ; |
116 | %% | 150 | %% |
117 | 151 | ||
152 | void yylex_init( yyscan_t * ); | ||
153 | void yylex_destroy( yyscan_t ); | ||
154 | |||
118 | int main() | 155 | int main() |
119 | { | 156 | { |
120 | yyparse(); | 157 | yyscan_t scanner; |
158 | |||
159 | yylex_init ( &scanner ); | ||
160 | yyparse( scanner ); | ||
161 | yylex_destroy ( scanner ); | ||
121 | return 0; | 162 | return 0; |
122 | } | 163 | } |
123 | 164 | ||