lrama 0.5.8 → 0.5.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/test.yaml +1 -1
- data/Gemfile +1 -1
- data/README.md +3 -3
- data/Rakefile +2 -7
- data/Steepfile +9 -1
- data/lib/lrama/command.rb +6 -1
- data/lib/lrama/grammar/percent_code.rb +12 -0
- data/lib/lrama/grammar/symbol.rb +2 -2
- data/lib/lrama/grammar.rb +100 -55
- data/lib/lrama/lexer/token/char.rb +8 -0
- data/lib/lrama/lexer/token/ident.rb +8 -0
- data/lib/lrama/lexer/token/parameterizing.rb +19 -0
- data/lib/lrama/lexer/token/tag.rb +8 -0
- data/lib/lrama/lexer/token/user_code.rb +14 -0
- data/lib/lrama/lexer/token.rb +9 -67
- data/lib/lrama/lexer.rb +14 -15
- data/lib/lrama/option_parser.rb +2 -1
- data/lib/lrama/options.rb +2 -1
- data/lib/lrama/output.rb +9 -0
- data/lib/lrama/parser.rb +500 -458
- data/lib/lrama/version.rb +1 -1
- data/parser.y +97 -73
- data/rbs_collection.lock.yaml +13 -1
- data/sample/calc.y +3 -1
- data/sample/parse.y +5 -1
- data/sig/lrama/grammar/percent_code.rbs +10 -0
- data/sig/lrama/grammar/reference.rbs +22 -0
- data/sig/lrama/grammar.rbs +5 -0
- data/sig/lrama/lexer/token/char.rbs +8 -0
- data/sig/lrama/lexer/token/ident.rbs +8 -0
- data/sig/lrama/lexer/token/parameterizing.rbs +8 -0
- data/sig/lrama/lexer/token/tag.rbs +8 -0
- data/sig/lrama/lexer/token/user_code.rbs +9 -0
- data/sig/lrama/lexer/token.rbs +17 -0
- data/template/bison/_yacc.h +2 -2
- data/template/bison/yacc.c +0 -2
- metadata +17 -4
- data/lib/lrama/lexer/token/type.rb +0 -8
- data/sig/lrama/lexer/token/type.rbs +0 -17
data/lib/lrama/lexer.rb
CHANGED
@@ -3,6 +3,7 @@ require "lrama/lexer/token"
|
|
3
3
|
|
4
4
|
module Lrama
|
5
5
|
class Lexer
|
6
|
+
attr_reader :head_line, :head_column
|
6
7
|
attr_accessor :status
|
7
8
|
attr_accessor :end_symbol
|
8
9
|
|
@@ -24,6 +25,8 @@ module Lrama
|
|
24
25
|
%precedence
|
25
26
|
%prec
|
26
27
|
%error-token
|
28
|
+
%empty
|
29
|
+
%code
|
27
30
|
)
|
28
31
|
|
29
32
|
def initialize(text)
|
@@ -63,8 +66,6 @@ module Lrama
|
|
63
66
|
when @scanner.scan(/\/\//)
|
64
67
|
@scanner.scan_until(/\n/)
|
65
68
|
newline
|
66
|
-
when @scanner.scan(/%empty/)
|
67
|
-
# noop
|
68
69
|
else
|
69
70
|
break
|
70
71
|
end
|
@@ -80,18 +81,20 @@ module Lrama
|
|
80
81
|
return [@scanner.matched, @scanner.matched]
|
81
82
|
when @scanner.scan(/#{PERCENT_TOKENS.join('|')}/)
|
82
83
|
return [@scanner.matched, @scanner.matched]
|
84
|
+
when @scanner.scan(/[\?\+\*]/)
|
85
|
+
return [@scanner.matched, @scanner.matched]
|
83
86
|
when @scanner.scan(/<\w+>/)
|
84
|
-
return [:TAG,
|
87
|
+
return [:TAG, setup_token(Lrama::Lexer::Token::Tag.new(s_value: @scanner.matched))]
|
85
88
|
when @scanner.scan(/'.'/)
|
86
|
-
return [:CHARACTER,
|
89
|
+
return [:CHARACTER, setup_token(Lrama::Lexer::Token::Char.new(s_value: @scanner.matched))]
|
87
90
|
when @scanner.scan(/'\\\\'|'\\b'|'\\t'|'\\f'|'\\r'|'\\n'|'\\v'|'\\13'/)
|
88
|
-
return [:CHARACTER,
|
91
|
+
return [:CHARACTER, setup_token(Lrama::Lexer::Token::Char.new(s_value: @scanner.matched))]
|
89
92
|
when @scanner.scan(/"/)
|
90
93
|
return [:STRING, %Q("#{@scanner.scan_until(/"/)})]
|
91
94
|
when @scanner.scan(/\d+/)
|
92
95
|
return [:INTEGER, Integer(@scanner.matched)]
|
93
96
|
when @scanner.scan(/([a-zA-Z_.][-a-zA-Z0-9_.]*)/)
|
94
|
-
token =
|
97
|
+
token = setup_token(Lrama::Lexer::Token::Ident.new(s_value: @scanner.matched))
|
95
98
|
type =
|
96
99
|
if @scanner.check(/\s*(\[\s*[a-zA-Z_.][-a-zA-Z0-9_.]*\s*\])?\s*:/)
|
97
100
|
:IDENT_COLON
|
@@ -100,7 +103,7 @@ module Lrama
|
|
100
103
|
end
|
101
104
|
return [type, token]
|
102
105
|
else
|
103
|
-
raise
|
106
|
+
raise ParseError, "Unexpected token: #{@scanner.peek(10).chomp}."
|
104
107
|
end
|
105
108
|
end
|
106
109
|
|
@@ -115,13 +118,13 @@ module Lrama
|
|
115
118
|
when @scanner.scan(/}/)
|
116
119
|
if nested == 0 && @end_symbol == '}'
|
117
120
|
@scanner.unscan
|
118
|
-
return [:C_DECLARATION,
|
121
|
+
return [:C_DECLARATION, setup_token(Lrama::Lexer::Token::UserCode.new(s_value: code))]
|
119
122
|
else
|
120
123
|
code += @scanner.matched
|
121
124
|
nested -= 1
|
122
125
|
end
|
123
126
|
when @scanner.check(/#{@end_symbol}/)
|
124
|
-
return [:C_DECLARATION,
|
127
|
+
return [:C_DECLARATION, setup_token(Lrama::Lexer::Token::UserCode.new(s_value: code))]
|
125
128
|
when @scanner.scan(/\n/)
|
126
129
|
code += @scanner.matched
|
127
130
|
newline
|
@@ -136,7 +139,7 @@ module Lrama
|
|
136
139
|
code += @scanner.getch
|
137
140
|
end
|
138
141
|
end
|
139
|
-
raise
|
142
|
+
raise ParseError, "Unexpected code: #{code}."
|
140
143
|
end
|
141
144
|
|
142
145
|
private
|
@@ -155,13 +158,9 @@ module Lrama
|
|
155
158
|
end
|
156
159
|
end
|
157
160
|
|
158
|
-
def
|
159
|
-
token = Token.new(type: type, s_value: s_value)
|
161
|
+
def setup_token(token)
|
160
162
|
token.line = @head_line
|
161
163
|
token.column = @head_column
|
162
|
-
options.each do |attr, value|
|
163
|
-
token.public_send("#{attr}=", value)
|
164
|
-
end
|
165
164
|
|
166
165
|
token
|
167
166
|
end
|
data/lib/lrama/option_parser.rb
CHANGED
@@ -58,6 +58,7 @@ module Lrama
|
|
58
58
|
o.separator 'Tuning the Parser:'
|
59
59
|
o.on('-S', '--skeleton=FILE', 'specify the skeleton to use') {|v| @options.skeleton = v }
|
60
60
|
o.on('-t', 'reserved, do nothing') { }
|
61
|
+
o.on('--debug', 'display debugging outputs of internal parser') {|v| @options.debug = true }
|
61
62
|
o.separator ''
|
62
63
|
o.separator 'Output:'
|
63
64
|
o.on('-H', '--header=[FILE]', 'also produce a header file named FILE') {|v| @options.header = true; @options.header_file = v }
|
@@ -108,7 +109,7 @@ module Lrama
|
|
108
109
|
def validate_trace(trace)
|
109
110
|
list = %w[
|
110
111
|
none locations scan parse automaton bitsets
|
111
|
-
closure grammar resource sets muscles tools
|
112
|
+
closure grammar rules resource sets muscles tools
|
112
113
|
m4-early m4 skeleton time ielr cex all
|
113
114
|
]
|
114
115
|
h = {}
|
data/lib/lrama/options.rb
CHANGED
@@ -4,7 +4,8 @@ module Lrama
|
|
4
4
|
attr_accessor :skeleton, :header, :header_file,
|
5
5
|
:report_file, :outfile,
|
6
6
|
:error_recovery, :grammar_file,
|
7
|
-
:report_file, :trace_opts, :report_opts, :y
|
7
|
+
:report_file, :trace_opts, :report_opts, :y,
|
8
|
+
:debug
|
8
9
|
|
9
10
|
def initialize
|
10
11
|
@skeleton = "bison/yacc.c"
|
data/lib/lrama/output.rb
CHANGED
@@ -349,6 +349,15 @@ module Lrama
|
|
349
349
|
end
|
350
350
|
end
|
351
351
|
|
352
|
+
# b4_percent_code_get
|
353
|
+
def percent_code(name)
|
354
|
+
@grammar.percent_codes.select do |percent_code|
|
355
|
+
percent_code.id.s_value == name
|
356
|
+
end.map do |percent_code|
|
357
|
+
percent_code.code.s_value
|
358
|
+
end.join
|
359
|
+
end
|
360
|
+
|
352
361
|
private
|
353
362
|
|
354
363
|
def eval_template(file, path)
|