-
Notifications
You must be signed in to change notification settings - Fork 36
/
como_lexer.l
136 lines (116 loc) · 3.82 KB
/
como_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
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
%{
/*
* Copyright (c) 2016 Ryan McCullagh <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "como_globals.h"
#include "como_ast.h"
#include "como_parser.h"
static void update_loc(YYLTYPE *loc, char* text)
{
loc->first_line = loc->last_line;
loc->first_column = loc->last_column;
int i;
for(i = 0; text[i] != '\0'; i++)
{
if(text[i] == '\n')
{
loc->last_line++;
loc->last_column = 0;
}
else
{
loc->last_column++;
}
}
}
#define YY_USER_ACTION update_loc(yylloc, yytext);
%}
%option outfile="como_lexer.c" header-file="como_lexer.h"
%option warn nodefault
%option reentrant noyywrap never-interactive nounistd yylineno
%option bison-bridge
%option bison-locations nounput noinput
%x COMMENT
WHITE_SPACE [ \r\n\t]*
L [a-zA-Z_]
A [a-zA-Z_0-9]
D [0-9]
%%
"//".*\n ;
"/*" BEGIN(COMMENT);
<COMMENT>"/*" ;
<COMMENT>"*/" BEGIN(INITIAL);
<COMMENT>"EOF" ;
<COMMENT>.|"\n" ;
"assert" { return T_ASSERT; }
"typeof" { return T_TYPEOF; }
"if" { return T_IF; }
"else" { return T_ELSE; }
"while" { return T_WHILE; }
"for" { return T_FOR; }
"func" { return T_FUNC; }
"function" { return T_FUNCTION; }
"print" { return T_PRINT; }
"return" { return T_RETURN; }
"==" { return T_CMP; }
"!=" { return T_NEQ; }
"<=" { return T_LTE; }
">=" { return T_GTE; }
"++" { return T_INC; }
"--" { return T_DEC; }
"&&" { return T_AND; }
{WHITE_SPACE} { /* Skipping Blanks Today */ }
{L}{A}* {
size_t len = strlen(yytext);
yylval->id = malloc(len + 1);
memcpy(yylval->id, yytext, len + 1);
return T_ID;
}
{D}+ {
yylval->number = strtol(yytext, NULL, 10);
return T_NUM;
}
L?\"(\\.|[^\\"])*\" {
size_t len = strlen(yytext);
if(len > 2U) {
yylval->stringliteral = malloc((len -2) + 1);
memcpy(yylval->stringliteral, yytext + 1, len - 2);
yylval->stringliteral[len - 2] = '\0';
return T_STR_LIT;
} else {
yylval->stringliteral = malloc(1);
yylval->stringliteral[0] = '\0';
return T_STR_LIT;
}
}
L?\'(\\.|[^\\'])*\' {
size_t len = strlen(yytext);
if(len > 2U) {
yylval->stringliteral = malloc((len -2) + 1);
memcpy(yylval->stringliteral, yytext + 1, len - 2);
yylval->stringliteral[len - 2] = '\0';
return T_STR_LIT;
} else {
yylval->stringliteral = malloc(1);
yylval->stringliteral[0] = '\0';
return T_STR_LIT;
}
}
. { return yytext[0]; }
%%