-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_builder_lexer.rb
64 lines (52 loc) · 1.39 KB
/
string_builder_lexer.rb
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
class StringBuilderLexer < RLTK::Lexer
# Keywords
rule(/if/) { :IF }
rule(/then/) { :THEN }
rule(/else/) { :ELSE }
rule(/end/) { :END }
rule(/while/) { :WHILE }
rule(/not/) { :NOT }
rule(/do/) { :DO }
rule(/readint/) { :READINT }
rule(/readstr/) { :READSTR }
rule(/begin/) { :BEGIN }
rule(/exit/) { :EXIT }
rule(/position/) { :POSITION }
# Boolean operators
rule(/and/) { :AND }
rule(/or/) { :OR }
# String methods
rule(/print/) { :PRINT }
rule(/length/) { :LENGTH }
rule(/concatenate/) { :CONCAT }
rule(/substring/) { :SUBSTR }
# String operators
rule(/==/) { :STR_COMPARE }
rule(/!=/) { :STR_DIFFRENT_COMPARE }
# Numeric operators
rule(/\+/) { :NUM_PLUS }
rule(/-/) { :NUM_MINUS }
rule(/\//) { :NUM_DIVIDE }
rule(/\*/) { :NUM_MULTIPLY }
rule(/\%/) { :NUM_MODULO }
rule(/<>/) { :DIFFRENT_THAN }
rule(/<=/) { :LESS_OR_EQUAL }
rule(/>=/) { :MORE_OR_EQUAL }
rule(/</) { :LESS_THAN }
rule(/>/) { :MORE_THAN }
rule(/=/) { :EQUALS_SIGN }
# Language
rule(/;/) { :SEMICOLON }
rule(/\(/) { :LPAREN }
rule(/\)/) { :RPAREN }
rule(/,/) { :COMMA }
rule(/:=/) { :ASSIGN }
rule(/true/) { :TRUE_BOOL }
rule(/false/) { :FALSE_BOOL }
rule(/[A-Za-z][A-Za-z0-9]*/) { |t| [:IDENT, t] }
# Number
rule(/\d+/) { |t| [:NUMBER, t.to_i] }
# String
rule(/"[^"|\n]*"/) { |t| [:TEXT, t.to_s[1..-2] ] }
rule(/\s/)
end