tmpl-expr-scanner.l 6.2 KB
Newer Older
Christian Hergert's avatar
Christian Hergert committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
%option reentrant
%option prefix="tmpl_expr_parser_"
%option bison-bridge
%option noyywrap
%option yylineno
%option nodefault

%option header-file="tmpl-expr-scanner.h"
%option outfile="tmpl-expr-scanner.c"

%{
# include "tmpl-error.h"
# include "tmpl-expr-private.h"
# include "tmpl-expr-parser-private.h"
# include "tmpl-expr-parser.h"
16
17
18

#pragma GCC diagnostic ignored "-Wswitch-default"
#pragma GCC diagnostic ignored "-Wunused-function"
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

static char *
copy_literal (const char *str)
{
  gsize len;
  if (str[0] == 'L')
    str++;
  g_assert (str[0] == '\"');
  str++;
  len = strlen (str);
  g_assert (len > 0);
  g_assert (str[len-1] == '\"');
  return g_strndup (str, len - 1);
}

Christian Hergert's avatar
Christian Hergert committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
%}

%option extra-type="TmplExprParser *"

 /* float exponent */
EXP ([Ee][-+]?[0-9]+)

%%

%{
TmplExprParser *parser = yyextra;
%}

 /* single character ops */
"+" |
"-" |
"*" |
"/" |
"=" |
"," |
"." |
55
"!" |
Christian Hergert's avatar
Christian Hergert committed
56
";" |
Christian Hergert's avatar
Christian Hergert committed
57
58
59
60
61
62
63
64
65
66
"(" |
")" { return yytext [0]; }

 /* comparison ops */
">"  { yylval->cmp = TMPL_EXPR_GT;  return CMP; }
"<"  { yylval->cmp = TMPL_EXPR_LT;  return CMP; }
"!=" { yylval->cmp = TMPL_EXPR_NE;  return CMP; }
"==" { yylval->cmp = TMPL_EXPR_EQ;  return CMP; }
">=" { yylval->cmp = TMPL_EXPR_GTE; return CMP; }
"<=" { yylval->cmp = TMPL_EXPR_LTE; return CMP; }
67
68
69
70

 /* boolean ops */
"||" { yylval->boolcmp = TMPL_EXPR_OR; return BOOLCMP; }
"&&" { yylval->boolcmp = TMPL_EXPR_AND; return BOOLCMP; }
Christian Hergert's avatar
Christian Hergert committed
71
72
73
74
75
76
77
78

 /* keywords */
"if"       { return IF; }
"then"     { return THEN; }
"else"     { return ELSE; }
"while"    { return WHILE; }
"do"       { return DO; }
"func"     { return FUNC; }
79
80
"def"      { return DEF; }
"end"      { return END; }
Christian Hergert's avatar
Christian Hergert committed
81
82
"require"  { return REQUIRE; }
"version"  { return VERSION; }
83
"pass"     { return NOP; }
Christian Hergert's avatar
Christian Hergert committed
84

85
 /* booleans */
Christian Hergert's avatar
Christian Hergert committed
86
87
"true"     { yylval->b = 1; return BOOL; }
"false"    { yylval->b = 0; return BOOL; }
88
89
90
91
"True"     { yylval->b = 1; return BOOL; }
"False"    { yylval->b = 0; return BOOL; }
"TRUE"     { yylval->b = 1; return BOOL; }
"FALSE"    { yylval->b = 0; return BOOL; }
Christian Hergert's avatar
Christian Hergert committed
92

93
94
95
 /* constants */
"null"     { return CONSTANT_NULL; }

Christian Hergert's avatar
Christian Hergert committed
96
 /* builtin functions */
97
98
99
100
101
102
103
"assert"   { yylval->fn = TMPL_EXPR_BUILTIN_ASSERT; return BUILTIN; }
"abs"      { yylval->fn = TMPL_EXPR_BUILTIN_ABS; return BUILTIN; }
"ceil"     { yylval->fn = TMPL_EXPR_BUILTIN_CEIL; return BUILTIN; }
"floor"    { yylval->fn = TMPL_EXPR_BUILTIN_FLOOR; return BUILTIN; }
"hex"      { yylval->fn = TMPL_EXPR_BUILTIN_HEX; return BUILTIN; }
"log"      { yylval->fn = TMPL_EXPR_BUILTIN_LOG; return BUILTIN; }
"print"    { yylval->fn = TMPL_EXPR_BUILTIN_PRINT; return BUILTIN; }
104
"printerr" { yylval->fn = TMPL_EXPR_BUILTIN_PRINTERR; return BUILTIN; }
105
106
107
108
109
110
"repr"     { yylval->fn = TMPL_EXPR_BUILTIN_REPR; return BUILTIN; }
"sqrt"     { yylval->fn = TMPL_EXPR_BUILTIN_SQRT; return BUILTIN; }
"typeof"   { yylval->fn = TMPL_EXPR_BUILTIN_TYPEOF; return BUILTIN; }
"sin"      { yylval->fn = TMPL_EXPR_BUILTIN_SIN; return BUILTIN; }
"tan"      { yylval->fn = TMPL_EXPR_BUILTIN_TAN; return BUILTIN; }
"cos"      { yylval->fn = TMPL_EXPR_BUILTIN_COS; return BUILTIN; }
111
112
113
114
115
116
117
118
"char"     { yylval->fn = TMPL_EXPR_BUILTIN_CAST_CHAR; return BUILTIN; }
"byte"     { yylval->fn = TMPL_EXPR_BUILTIN_CAST_BYTE; return BUILTIN; }
"i32"      { yylval->fn = TMPL_EXPR_BUILTIN_CAST_I32; return BUILTIN; }
"u32"      { yylval->fn = TMPL_EXPR_BUILTIN_CAST_U32; return BUILTIN; }
"i64"      { yylval->fn = TMPL_EXPR_BUILTIN_CAST_I64; return BUILTIN; }
"u64"      { yylval->fn = TMPL_EXPR_BUILTIN_CAST_U64; return BUILTIN; }
"float"    { yylval->fn = TMPL_EXPR_BUILTIN_CAST_FLOAT; return BUILTIN; }
"double"   { yylval->fn = TMPL_EXPR_BUILTIN_CAST_DOUBLE; return BUILTIN; }
119
"bool"     { yylval->fn = TMPL_EXPR_BUILTIN_CAST_BOOL; return BUILTIN; }
Christian Hergert's avatar
Christian Hergert committed
120
121

 /* string literals */
122
L?\"(\\.|[^\\"])*\" { yylval->s = copy_literal (yytext); return STRING_LITERAL; }
Christian Hergert's avatar
Christian Hergert committed
123

124
125
126
127
128
129
 /* @ to escape reserved keywords */
@[a-zA-Z_][a-zA-Z0-9_]* {
  yylval->s = g_strdup (yytext+1);
  return NAME;
}

Christian Hergert's avatar
Christian Hergert committed
130
131
132
133
134
135
136
137
138
139
 /* names */
[a-zA-Z_][a-zA-Z0-9_]* {
  yylval->s = g_strdup (yytext);
  return NAME;
}

[0-9]+"."[0-9]*{EXP}? |
"."?[0-9]+{EXP}? { yylval->d = atof(yytext); return NUMBER; }

"//".*
140
[ \t]   /* ignore whitespace */
141
"\\\n"  /* ignore escpaed newlines */
142
#[^\n]* { return EOL; } /* line-wise comments should EOL things */
Christian Hergert's avatar
Christian Hergert committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"\n"    { return EOL; }
<<EOF>> { parser->reached_eof = TRUE; return EOL; }
.       {
          g_free (parser->error_str);
          parser->error_str = g_strdup (yytext);
          parser->error_line = yylineno;
        }

%%

void
tmpl_expr_parser_init_scanner (TmplExprParser *parser)
{
  g_assert (parser != NULL);

  yylex_init (&parser->scanner);
  yyset_extra (parser, parser->scanner);
}

void
tmpl_expr_parser_destroy_scanner (TmplExprParser *parser)
{
  g_assert (parser != NULL);

  yylex_destroy (parser->scanner);
}

void
tmpl_expr_parser_flush (TmplExprParser *parser)
{
  struct yyguts_t *yyg;

  g_assert (parser != NULL);

  yyg = (struct yyguts_t *)parser->scanner;
  tmpl_expr_parser__flush_buffer (YY_CURRENT_BUFFER, parser->scanner);
}

gboolean
tmpl_expr_parser_parse_string (TmplExprParser  *self,
                               const gchar    *input,
                               GError        **error)
{
  YY_BUFFER_STATE buf;
187
  int ret = 0;
Christian Hergert's avatar
Christian Hergert committed
188
189
190
191

  g_return_val_if_fail (self != NULL, FALSE);

  buf = tmpl_expr_parser__scan_string (input, self->scanner);
192
193
  while (!self->reached_eof && ret == 0)
    ret = tmpl_expr_parser_parse (self);
Christian Hergert's avatar
Christian Hergert committed
194
195
  tmpl_expr_parser__delete_buffer (buf, self->scanner);

196
197
198
  if (ret == 0 && self->error_str == NULL)
    return TRUE;

Christian Hergert's avatar
Christian Hergert committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
  if (self->error_str)
    {
      g_set_error (error,
                   TMPL_ERROR,
                   TMPL_ERROR_SYNTAX_ERROR,
                   "%s", self->error_str);
      return FALSE;
    }

  g_set_error (error,
               TMPL_ERROR,
               TMPL_ERROR_SYNTAX_ERROR,
               "Failed to parse expression");

  return FALSE;
}

gboolean
tmpl_expr_parser_init (TmplExprParser  *self,
                       GError        **error)
{
  g_return_val_if_fail (self != NULL, FALSE);

  memset (self, 0, sizeof *self);
  tmpl_expr_parser_init_scanner (self);

  return TRUE;
}

void
tmpl_expr_parser_destroy (TmplExprParser *self)
{
  if (self != NULL)
    {
      tmpl_expr_parser_destroy_scanner (self);
      g_clear_pointer (&self->ast, tmpl_expr_unref);
      g_clear_pointer (&self->error_str, g_free);
    }
}