tmpl-expr-scanner.l 4.27 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"
Christian Hergert's avatar
Christian Hergert committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
%}

%option extra-type="TmplExprParser *"

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

%%

%{
TmplExprParser *parser = yyextra;
%}

 /* single character ops */
"+" |
"-" |
"*" |
"/" |
"=" |
"," |
"." |
";" |
41
"!" |
Christian Hergert's avatar
Christian Hergert committed
42 43 44 45 46 47 48 49 50 51
"(" |
")" { 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; }
52 53
"||" { yylval->cmp = TMPL_EXPR_OR; return CMP; }
"&&" { yylval->cmp = TMPL_EXPR_AND; return CMP; }
Christian Hergert's avatar
Christian Hergert committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

 /* keywords */
"if"       { return IF; }
"then"     { return THEN; }
"else"     { return ELSE; }
"while"    { return WHILE; }
"do"       { return DO; }
"func"     { return FUNC; }
"require"  { return REQUIRE; }
"version"  { return VERSION; }

"true"     { yylval->b = 1; return BOOL; }
"false"    { yylval->b = 0; return BOOL; }

 /* builtin functions */
69 70 71 72 73 74 75 76
"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; }
"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; }
Christian Hergert's avatar
Christian Hergert committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 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

 /* string literals */
L?\"(\\.|[^\\"])*\" { yylval->s = yytext; return STRING_LITERAL; }

 /* 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; }

"//".*
[ \t] /* ignore whitespace */
\\n     { printf ("c> "); }
"\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;
  gint ret;

  g_return_val_if_fail (self != NULL, FALSE);

  buf = tmpl_expr_parser__scan_string (input, self->scanner);
  ret = tmpl_expr_parser_parse (self);
  tmpl_expr_parser__delete_buffer (buf, self->scanner);

  if (self->error_str)
    {
      g_set_error (error,
                   TMPL_ERROR,
                   TMPL_ERROR_SYNTAX_ERROR,
                   "%s", self->error_str);
      return FALSE;
    }

  if (ret == 0)
    return TRUE;

  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);
    }
}