diff options
Diffstat (limited to 'LuaSL/src/LuaSL_lexer.l')
-rw-r--r-- | LuaSL/src/LuaSL_lexer.l | 166 |
1 files changed, 78 insertions, 88 deletions
diff --git a/LuaSL/src/LuaSL_lexer.l b/LuaSL/src/LuaSL_lexer.l index 50f2624..b4a1444 100644 --- a/LuaSL/src/LuaSL_lexer.l +++ b/LuaSL/src/LuaSL_lexer.l | |||
@@ -4,7 +4,7 @@ | |||
4 | #include "LuaSL_LSL_tree.h" | 4 | #include "LuaSL_LSL_tree.h" |
5 | #include <stdio.h> | 5 | #include <stdio.h> |
6 | 6 | ||
7 | int common(YYSTYPE *lval, char *text, boolean checkIgnorable, int type); | 7 | int common(YYSTYPE *lval, char *text, LuaSL_yyparseExtra *extra, boolean checkIgnorable, int type); |
8 | 8 | ||
9 | %} | 9 | %} |
10 | 10 | ||
@@ -13,6 +13,7 @@ int common(YYSTYPE *lval, char *text, boolean checkIgnorable, int type); | |||
13 | %option noreject noyymore | 13 | %option noreject noyymore |
14 | %option backup debug perf-report perf-report verbose warn | 14 | %option backup debug perf-report perf-report verbose warn |
15 | %option align full | 15 | %option align full |
16 | %option extra-type="LuaSL_yyparseExtra *" | ||
16 | 17 | ||
17 | HEX [[:xdigit:]] | 18 | HEX [[:xdigit:]] |
18 | INTEGER [[:digit:]]+ | 19 | INTEGER [[:digit:]]+ |
@@ -27,138 +28,127 @@ IDENTIFIER [[:alpha:]](_|[[:alpha:]]|[[:digit:]])* | |||
27 | /* The order here is important, in mysterious ways. The more specific the lower in case of ambiguities like "floats contain integers". I think, not tested that well yet. */ | 28 | /* The order here is important, in mysterious ways. The more specific the lower in case of ambiguities like "floats contain integers". I think, not tested that well yet. */ |
28 | 29 | ||
29 | /* Ignorables. */ | 30 | /* Ignorables. */ |
30 | [[:space:]]+ %{ common(yylval, yytext, FALSE, LSL_SPACE); %} | 31 | [[:space:]]+ %{ common(yylval, yytext, yyextra, FALSE, LSL_SPACE); %} |
31 | /* Yes I know this will have problems with huge comments, just being simple to get it to work for now. */ | 32 | /* Yes I know this will have problems with huge comments, just being simple to get it to work for now. */ |
32 | "/*"([^"*/"]*)"*/" %{ common(yylval, yytext, FALSE, LSL_COMMENT); %} | 33 | "/*"([^"*/"]*)"*/" %{ common(yylval, yytext, yyextra, FALSE, LSL_COMMENT); %} |
33 | "//"[^\n]* %{ common(yylval, yytext, FALSE, LSL_COMMENT_LINE); %} | 34 | "//"[^\n]* %{ common(yylval, yytext, yyextra, FALSE, LSL_COMMENT_LINE); %} |
34 | 35 | ||
35 | /* Operations. */ | 36 | /* Operations. */ |
36 | "&&" { return common(yylval, yytext, TRUE, LSL_BOOL_AND); } | 37 | "&&" { return common(yylval, yytext, yyextra, TRUE, LSL_BOOL_AND); } |
37 | "||" { return common(yylval, yytext, TRUE, LSL_BOOL_OR); } | 38 | "||" { return common(yylval, yytext, yyextra, TRUE, LSL_BOOL_OR); } |
38 | "|" { return common(yylval, yytext, TRUE, LSL_BIT_OR); } | 39 | "|" { return common(yylval, yytext, yyextra, TRUE, LSL_BIT_OR); } |
39 | "^" { return common(yylval, yytext, TRUE, LSL_BIT_XOR); } | 40 | "^" { return common(yylval, yytext, yyextra, TRUE, LSL_BIT_XOR); } |
40 | "&" { return common(yylval, yytext, TRUE, LSL_BIT_AND); } | 41 | "&" { return common(yylval, yytext, yyextra, TRUE, LSL_BIT_AND); } |
41 | "!=" { return common(yylval, yytext, TRUE, LSL_NOT_EQUAL); } | 42 | "!=" { return common(yylval, yytext, yyextra, TRUE, LSL_NOT_EQUAL); } |
42 | "==" { return common(yylval, yytext, TRUE, LSL_EQUAL); } | 43 | "==" { return common(yylval, yytext, yyextra, TRUE, LSL_EQUAL); } |
43 | ">=" { return common(yylval, yytext, TRUE, LSL_GREATER_EQUAL); } | 44 | ">=" { return common(yylval, yytext, yyextra, TRUE, LSL_GREATER_EQUAL); } |
44 | "<=" { return common(yylval, yytext, TRUE, LSL_LESS_EQUAL); } | 45 | "<=" { return common(yylval, yytext, yyextra, TRUE, LSL_LESS_EQUAL); } |
45 | ">" { return common(yylval, yytext, TRUE, LSL_GREATER_THAN); } | 46 | ">" { return common(yylval, yytext, yyextra, TRUE, LSL_GREATER_THAN); } |
46 | "<" { return common(yylval, yytext, TRUE, LSL_LESS_THAN); } | 47 | "<" { return common(yylval, yytext, yyextra, TRUE, LSL_LESS_THAN); } |
47 | ">>" { return common(yylval, yytext, TRUE, LSL_RIGHT_SHIFT); } | 48 | ">>" { return common(yylval, yytext, yyextra, TRUE, LSL_RIGHT_SHIFT); } |
48 | "<<" { return common(yylval, yytext, TRUE, LSL_LEFT_SHIFT); } | 49 | "<<" { return common(yylval, yytext, yyextra, TRUE, LSL_LEFT_SHIFT); } |
49 | "+" { return common(yylval, yytext, TRUE, LSL_ADD); } | 50 | "+" { return common(yylval, yytext, yyextra, TRUE, LSL_ADD); } |
50 | "-" { return common(yylval, yytext, TRUE, LSL_SUBTRACT); } | 51 | "-" { return common(yylval, yytext, yyextra, TRUE, LSL_SUBTRACT); } |
51 | "*" { return common(yylval, yytext, TRUE, LSL_MULTIPLY); } | 52 | "*" { return common(yylval, yytext, yyextra, TRUE, LSL_MULTIPLY); } |
52 | "%" { return common(yylval, yytext, TRUE, LSL_MODULO); } | 53 | "%" { return common(yylval, yytext, yyextra, TRUE, LSL_MODULO); } |
53 | "/" { return common(yylval, yytext, TRUE, LSL_DIVIDE); } | 54 | "/" { return common(yylval, yytext, yyextra, TRUE, LSL_DIVIDE); } |
54 | "!" { return common(yylval, yytext, TRUE, LSL_BOOL_NOT); } | 55 | "!" { return common(yylval, yytext, yyextra, TRUE, LSL_BOOL_NOT); } |
55 | "~" { return common(yylval, yytext, TRUE, LSL_BIT_NOT); } | 56 | "~" { return common(yylval, yytext, yyextra, TRUE, LSL_BIT_NOT); } |
56 | "[" { return common(yylval, yytext, TRUE, LSL_BRACKET_OPEN); } | 57 | "[" { return common(yylval, yytext, yyextra, TRUE, LSL_BRACKET_OPEN); } |
57 | "]" { return common(yylval, yytext, TRUE, LSL_BRACKET_CLOSE); } | 58 | "]" { return common(yylval, yytext, yyextra, TRUE, LSL_BRACKET_CLOSE); } |
58 | "(" { return common(yylval, yytext, TRUE, LSL_PARENTHESIS_OPEN); } | 59 | "(" { return common(yylval, yytext, yyextra, TRUE, LSL_PARENTHESIS_OPEN); } |
59 | ")" { return common(yylval, yytext, TRUE, LSL_PARENTHESIS_CLOSE); } | 60 | ")" { return common(yylval, yytext, yyextra, TRUE, LSL_PARENTHESIS_CLOSE); } |
60 | "+=" { return common(yylval, yytext, TRUE, LSL_ASSIGNMENT_ADD); } | 61 | "+=" { return common(yylval, yytext, yyextra, TRUE, LSL_ASSIGNMENT_ADD); } |
61 | "-=" { return common(yylval, yytext, TRUE, LSL_ASSIGNMENT_SUBTRACT); } | 62 | "-=" { return common(yylval, yytext, yyextra, TRUE, LSL_ASSIGNMENT_SUBTRACT); } |
62 | "*=" { return common(yylval, yytext, TRUE, LSL_ASSIGNMENT_MULTIPLY); } | 63 | "*=" { return common(yylval, yytext, yyextra, TRUE, LSL_ASSIGNMENT_MULTIPLY); } |
63 | "%=" { return common(yylval, yytext, TRUE, LSL_ASSIGNMENT_MODULO); } | 64 | "%=" { return common(yylval, yytext, yyextra, TRUE, LSL_ASSIGNMENT_MODULO); } |
64 | "/=" { return common(yylval, yytext, TRUE, LSL_ASSIGNMENT_DIVIDE); } | 65 | "/=" { return common(yylval, yytext, yyextra, TRUE, LSL_ASSIGNMENT_DIVIDE); } |
65 | "=" { return common(yylval, yytext, TRUE, LSL_ASSIGNMENT_PLAIN); } | 66 | "=" { return common(yylval, yytext, yyextra, TRUE, LSL_ASSIGNMENT_PLAIN); } |
66 | "." { return common(yylval, yytext, TRUE, LSL_DOT); } | 67 | "." { return common(yylval, yytext, yyextra, TRUE, LSL_DOT); } |
67 | "--" { return common(yylval, yytext, TRUE, LSL_DECREMENT_PRE); } | 68 | "--" { return common(yylval, yytext, yyextra, TRUE, LSL_DECREMENT_PRE); } |
68 | "++" { return common(yylval, yytext, TRUE, LSL_INCREMENT_PRE); } | 69 | "++" { return common(yylval, yytext, yyextra, TRUE, LSL_INCREMENT_PRE); } |
69 | "," { return common(yylval, yytext, TRUE, LSL_COMMA); } | 70 | "," { return common(yylval, yytext, yyextra, TRUE, LSL_COMMA); } |
70 | 71 | ||
71 | /* Types. */ | 72 | /* Types. */ |
72 | {INTEGER} %{ yylval->value.integerValue = atoi(yytext); return common(yylval, yytext, TRUE, LSL_INTEGER); %} | 73 | {INTEGER} %{ yylval->value.integerValue = atoi(yytext); return common(yylval, yytext, yyextra, TRUE, LSL_INTEGER); %} |
73 | {FLOAT} %{ yylval->value.floatValue = atof(yytext); return common(yylval, yytext, TRUE, LSL_FLOAT); %} | 74 | {FLOAT} %{ yylval->value.floatValue = atof(yytext); return common(yylval, yytext, yyextra, TRUE, LSL_FLOAT); %} |
74 | 75 | ||
75 | /* Type keywords. */ | 76 | /* Type keywords. */ |
76 | "float" %{ return common(yylval, yytext, TRUE, LSL_TYPE_FLOAT); %} | 77 | "float" %{ return common(yylval, yytext, yyextra, TRUE, LSL_TYPE_FLOAT); %} |
77 | "integer" %{ return common(yylval, yytext, TRUE, LSL_TYPE_INTEGER); %} | 78 | "integer" %{ return common(yylval, yytext, yyextra, TRUE, LSL_TYPE_INTEGER); %} |
78 | "key" %{ return common(yylval, yytext, TRUE, LSL_TYPE_KEY); %} | 79 | "key" %{ return common(yylval, yytext, yyextra, TRUE, LSL_TYPE_KEY); %} |
79 | "list" %{ return common(yylval, yytext, TRUE, LSL_TYPE_LIST); %} | 80 | "list" %{ return common(yylval, yytext, yyextra, TRUE, LSL_TYPE_LIST); %} |
80 | "quaternion" %{ return common(yylval, yytext, TRUE, LSL_TYPE_ROTATION); %} | 81 | "quaternion" %{ return common(yylval, yytext, yyextra, TRUE, LSL_TYPE_ROTATION); %} |
81 | "rotation" %{ return common(yylval, yytext, TRUE, LSL_TYPE_ROTATION); %} | 82 | "rotation" %{ return common(yylval, yytext, yyextra, TRUE, LSL_TYPE_ROTATION); %} |
82 | "string" %{ return common(yylval, yytext, TRUE, LSL_TYPE_STRING); %} | 83 | "string" %{ return common(yylval, yytext, yyextra, TRUE, LSL_TYPE_STRING); %} |
83 | "vector" %{ return common(yylval, yytext, TRUE, LSL_TYPE_VECTOR); %} | 84 | "vector" %{ return common(yylval, yytext, yyextra, TRUE, LSL_TYPE_VECTOR); %} |
84 | 85 | ||
85 | /* Statement keywords. */ | 86 | /* Statement keywords. */ |
86 | "do" %{ return common(yylval, yytext, TRUE, LSL_DO); %} | 87 | "do" %{ return common(yylval, yytext, yyextra, TRUE, LSL_DO); %} |
87 | "for" %{ return common(yylval, yytext, TRUE, LSL_FOR); %} | 88 | "for" %{ return common(yylval, yytext, yyextra, TRUE, LSL_FOR); %} |
88 | "else" %{ return common(yylval, yytext, TRUE, LSL_ELSE); %} | 89 | "else" %{ return common(yylval, yytext, yyextra, TRUE, LSL_ELSE); %} |
89 | "if" %{ return common(yylval, yytext, TRUE, LSL_IF); %} | 90 | "if" %{ return common(yylval, yytext, yyextra, TRUE, LSL_IF); %} |
90 | "jump" %{ return common(yylval, yytext, TRUE, LSL_JUMP); %} | 91 | "jump" %{ return common(yylval, yytext, yyextra, TRUE, LSL_JUMP); %} |
91 | "return" %{ return common(yylval, yytext, TRUE, LSL_RETURN); %} | 92 | "return" %{ return common(yylval, yytext, yyextra, TRUE, LSL_RETURN); %} |
92 | "state" %{ return common(yylval, yytext, TRUE, LSL_STATE_CHANGE); %} | 93 | "state" %{ return common(yylval, yytext, yyextra, TRUE, LSL_STATE_CHANGE); %} |
93 | "while" %{ return common(yylval, yytext, TRUE, LSL_WHILE); %} | 94 | "while" %{ return common(yylval, yytext, yyextra, TRUE, LSL_WHILE); %} |
94 | 95 | ||
95 | {IDENTIFIER} %{ /* yylval->value.identifierValue = strdup(yytext); */ common(yylval, yytext, TRUE, LSL_IDENTIFIER); %} | 96 | {IDENTIFIER} %{ /* yylval->value.identifierValue = strdup(yytext); */ common(yylval, yytext, yyextra, TRUE, LSL_IDENTIFIER); %} |
96 | 97 | ||
97 | /* Other symbols. */ | 98 | /* Other symbols. */ |
98 | "@" %{ return common(yylval, yytext, TRUE, LSL_LABEL); %} | 99 | "@" %{ return common(yylval, yytext, yyextra, TRUE, LSL_LABEL); %} |
99 | "{" %{ return common(yylval, yytext, TRUE, LSL_BLOCK_OPEN); %} | 100 | "{" %{ return common(yylval, yytext, yyextra, TRUE, LSL_BLOCK_OPEN); %} |
100 | "}" %{ return common(yylval, yytext, TRUE, LSL_BLOCK_CLOSE); %} | 101 | "}" %{ return common(yylval, yytext, yyextra, TRUE, LSL_BLOCK_CLOSE); %} |
101 | ";" %{ return common(yylval, yytext, TRUE, LSL_STATEMENT); %} | 102 | ";" %{ return common(yylval, yytext, yyextra, TRUE, LSL_STATEMENT); %} |
102 | 103 | ||
103 | <<EOF>> { return common(yylval, yytext, TRUE, LSL_SCRIPT); } | 104 | <<EOF>> { return common(yylval, yytext, yyextra, TRUE, LSL_SCRIPT); } |
104 | 105 | ||
105 | /* Everything else */ | 106 | /* Everything else */ |
106 | . %{ printf(" unexpected character.\n"); yylval->value.unknownValue = strdup(yytext); common(yylval, yytext, TRUE, LSL_UNKNOWN); %} | 107 | . %{ printf(" unexpected character.\n"); yylval->value.unknownValue = strdup(yytext); common(yylval, yytext, yyextra, TRUE, LSL_UNKNOWN); %} |
107 | 108 | ||
108 | %% | 109 | %% |
109 | 110 | ||
110 | // TODO - this is not reentrant, should make it so. | 111 | int common(YYSTYPE *lval, char *text, LuaSL_yyparseExtra *extra, boolean checkIgnorable, int type) |
111 | static char *ignorableText = NULL; | ||
112 | static int column = 0; | ||
113 | static int line = 0; | ||
114 | |||
115 | int common(YYSTYPE *lval, char *text, boolean checkIgnorable, int type) | ||
116 | { | 112 | { |
117 | int i; | 113 | int i; |
118 | 114 | ||
119 | for (i = 0; text[i] != '\0'; i++) | 115 | for (i = 0; text[i] != '\0'; i++) |
120 | if (text[i] == '\n') | 116 | if (text[i] == '\n') |
121 | { | 117 | { |
122 | column = 0; | 118 | extra->column = 0; |
123 | line++; | 119 | extra->line++; |
124 | } | 120 | } |
125 | else if (text[i] == '\t') | 121 | else if (text[i] == '\t') |
126 | column += 8 - (column % 8); | 122 | extra->column += 8 - (extra->column % 8); |
127 | else | 123 | else |
128 | column++; | 124 | extra->column++; |
129 | 125 | ||
130 | lval->token = tokens[type - lowestToken]; | 126 | lval->token = tokens[type - lowestToken]; |
131 | lval->line = line; | 127 | lval->line = extra->line; |
132 | lval->column = column; | 128 | lval->column = extra->column; |
133 | 129 | ||
134 | if (checkIgnorable) | 130 | if (checkIgnorable) |
135 | { | 131 | { |
136 | lval->ignorableText = ignorableText; | 132 | lval->ignorableText = extra->ignorableText; |
137 | ignorableText = NULL; | 133 | extra->ignorableText = NULL; |
138 | } | 134 | } |
139 | else | 135 | else |
140 | { | 136 | { |
141 | if (ignorableText) | 137 | if (extra->ignorableText) |
142 | { | 138 | { |
143 | int lenI = strlen(ignorableText); | 139 | int lenI = strlen(extra->ignorableText); |
144 | int lenT = strlen(text); | 140 | int lenT = strlen(text); |
145 | 141 | ||
146 | ignorableText = realloc(ignorableText, lenI + lenT + 1); | 142 | extra->ignorableText = realloc(extra->ignorableText, lenI + lenT + 1); |
147 | sprintf(&ignorableText[lenI], "%s", text); | 143 | sprintf(&(extra->ignorableText[lenI]), "%s", text); |
148 | } | 144 | } |
149 | else | 145 | else |
150 | ignorableText = strdup(text); | 146 | extra->ignorableText = strdup(text); |
151 | } | 147 | } |
152 | 148 | ||
153 | return type; | 149 | return type; |
154 | } | 150 | } |
155 | 151 | ||
156 | int yyerror(const char *msg) | ||
157 | { | ||
158 | fprintf(stderr, "Parser error on line %d, column %d: %s\n", line, column, msg); | ||
159 | return 0; | ||
160 | } | ||
161 | |||
162 | int yywrap(yyscan_t yyscanner) | 152 | int yywrap(yyscan_t yyscanner) |
163 | { | 153 | { |
164 | #ifdef FLEX_SCANNER | 154 | #ifdef FLEX_SCANNER |