lexer.l (1585B)
1 %option noyywrap 2 3 %{ 4 #include <stdio.h> 5 #include "num.h" 6 7 #define YY_DECL int yylex() 8 9 #include "parser.tab.h" 10 11 static char * copy_num(char *in, int len) { 12 static char buf[2048] = {0}; 13 int i, j = 0; 14 15 for (int i = 0; i < len; i++) { 16 char c = in[i]; 17 if (c != ',' && c != '_') 18 buf[j++] = c; 19 } 20 buf[j] = 0; 21 22 return buf; 23 } 24 25 26 %} 27 28 %% 29 30 [ \t] ; // ignore all whitespace 31 32 [bB][iI][tT][sS]? { 33 yylval.lexunit.unit = UNIT_BITS; 34 yylval.lexunit.name = "bits"; 35 return T_UNIT; 36 } 37 38 in|IN|to|TO { 39 return T_IN; 40 } 41 42 [mM]?[bB][tT][cC] { 43 int ism = yytext[0] == 'm' || yytext[0] == 'M'; 44 yylval.lexunit.unit = ism? UNIT_MBTC : UNIT_BTC; 45 yylval.lexunit.name = ism? "mBTC" : "BTC"; 46 return T_UNIT; 47 } 48 49 fiat|FIAT|alt|ALT|usd|USD|cur|CUR|other|OTHER { 50 yylval.lexunit.unit = UNIT_OTHER; 51 yylval.lexunit.name = yytext; 52 return T_UNIT; 53 } 54 55 [fF][iI][nN][nN][eE][yY]?[sS]? { 56 yylval.lexunit.unit = UNIT_FINNEY; 57 yylval.lexunit.name = "finneys"; 58 return T_UNIT; 59 } 60 61 [mM]?[sS][aA][tT][sS]? { 62 int ism = yytext[0] == 'm' || yytext[0] == 'M'; 63 yylval.lexunit.unit = ism ? UNIT_MSATOSHI : UNIT_SATOSHI; 64 yylval.lexunit.name = ism ? "msats" : "sats"; 65 return T_UNIT; 66 } 67 68 [0-9]*\.[0-9]+ { 69 double d = atof(yytext); 70 yylval.floatval = d; 71 return T_FLOAT; 72 } 73 74 [0-9_,]+ { 75 yylval.intval = strtoll(copy_num(yytext, yyleng), NULL, 10); 76 return T_INT; 77 } 78 79 \n {return T_NEWLINE;} 80 "+" {return T_PLUS;} 81 "-" {return T_MINUS;} 82 "*" {return T_MULTIPLY;} 83 "/" {return T_DIVIDE;} 84 "(" {return T_LEFT;} 85 ")" {return T_RIGHT;} 86 87 %%