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