GCC Rust: Parsing in floats now
[official-gcc.git] / gcc / rust / rs-lexer.l
blobb45f9317476b5a40aeae96c13e3a001920409736
1 %{
2   /* This file is part of GCC.
4      GCC is free software; you can redistribute it and/or modify it under
5      the terms of the GNU General Public License as published by the Free
6      Software Foundation; either version 3, or (at your option) any later
7      version.
9      GCC is distributed in the hope that it will be useful, but WITHOUT ANY
10      WARRANTY; without even the implied warranty of MERCHANTABILITY or
11      FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12      for more details.
14      You should have received a copy of the GNU General Public License
15      along with GCC; see the file COPYING3.  If not see
16      <http://www.gnu.org/licenses/>.  */
18 #include "rust.h"
19 #include "y.rs.h"
21 extern int yydebug;
22 extern int yyparse (void);
25 DIGIT        [0-9]
26 ID           [_a-zA-Z][a-zA_Z0-9_$]*
27 QSTRING      \"[^\"\n]*[\"\n]
29 %x comment
30 %option yylineno noyywrap nounput 8bit never-interactive
33 "/*"                    {  BEGIN (comment); }
34 <comment>[^*\n]*        {  /* eat it up flex you be le hungry maybe? */ }
35 <comment>"*"+[^*/\n]*   {  /* eat up boyo */ }
36 <comment>\n             {  ++yylineno; }
37 <comment>"*"+"/"        {  BEGIN (INITIAL); }
39 as                      {  return AS; }
40 pub                     {  return PUB; }
41 break                   {  return BREAK; }
42 return                  {  return RETURN; }
43 continue                {  return CONTINUE; }
44 do                      {  return DO; }
45 fn                      {  return DEFUN; }
46 let                     {  return LET; }
47 mut                     {  return MUT; }
48 while                   {  return WHILE; }
49 loop                    {  return LOOP; }
50 static                  {  return STATIC; }
51 struct                  {  return STRUCT; }
52 if                      {  return IF; }
53 "else if"               {  return ELIF; }
54 else                    {  return ELSE; }
55 impl                    {  return IMPL; }
56 self                    {  return SELF; }
57 bool                    {  return TYPE_BOOL; }
58 int                     {  return TYPE_INT; }
59 float                   {  return TYPE_FLOAT; }
60 uint                    {  return TYPE_UINT; }
61 enum                    {  return ENUM; }
62 match                   {  return MATCH; }
63 true                    {  return XTRUE; }
64 false                   {  return XFALSE; }
65 trait                   {  return TRAIT; }
66 for                     {  return FOR; }
67 \[                      {  return '['; }
68 \]                      {  return ']'; }
69 \(                      {  return '('; }
70 \)                      {  return ')'; }
71 \{                      {  return '{'; }
72 \}                      {  return '}'; }
73 "->"                    {  return RTYPE; }
74 "=>"                    {  return GOES; }
75 ";"                     { return ';'; }
76 ","                     { return ','; }
77 "."                     { return '.'; }
78 ":"                     { return ':'; }
79 "::"                    { return ACC; }
80 "="                     { return '='; }
81 "+"                     { return '+'; }
82 "-"                     { return '-'; }
83 "/"                     { return '/'; }
84 "*"                     { return '*'; }
85 "|"                     { return '|'; }
86 "~"                     { return '~'; }
87 "&"                     { return '&'; }
88 "=="                    { return EQUAL_EQUAL; }
89 "!="                    { return NOT_EQUAL; }
90 "<"                     { return '<'; }
91 "<="                    { return LESS_EQUAL; }
92 ">"                     { return '>'; }
93 ">="                    { return GREATER_EQUAL; }
95 \/\/.*                  ;  /* // style comment */
97 {QSTRING}               {
98   yylval.string = xstrdup (yytext + 1);
99   if (yylval.string [yyleng - 2] != '\"')
100     error ("Un-termintated character string!\n");
101   else
102     yylval.string [yyleng - 2] = '\0';
103   return STRING;
106 {DIGIT}+                {
107   mpfr_t x;
108   mpfr_init2 (x, 32);
109   if (mpfr_set_str (x, yytext, 10, GMP_RNDU))
110     {
111       fatal_error ("error initilizing integer value <%s>!\n", yytext);
112     }
113   yylval.integer = mpfr_get_si (x, GMP_RNDU);
114   mpfr_clear (x);
115   return INTEGER;
118 {DIGIT}+"."{DIGIT}+    {
119   mpfr_t x;
120   mpfr_init2 (x, 32);
121   if (mpfr_set_str (x, yytext, 10, GMP_RNDU))
122     {
123       fatal_error ("error initilizing float value <%s>!\n", yytext);
124     }
125   yylval.ffloat = mpfr_get_flt (x, GMP_RNDU);
126   mpfr_clear (x);
127   return FLOAT;
130 {ID}                    {
131   yylval.string = xstrdup (yytext);
132   return IDENTIFIER;
135 .           {  }
136 \n          {  }
140 bool grs_do_compile (const char * in)
142     bool retval = true;
143     FILE * fd = fopen (in, "rb");
144     if (fd)
145     {
146         yyin = fd;
147         // yydebug = 1;
148         linemap_add (line_table, LC_ENTER, 0, in, 0);
149         retval = yyparse ();
150         fclose (fd);
151         linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
152         yylex_destroy ();
153     }
154     else
155     {
156         fprintf (stderr, "error opening file %s\n", in);
157         retval = false;
158     }
159     return retval;