rley 0.5.02 → 0.5.03
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -0
- data/CHANGELOG.md +5 -0
- data/examples/data_formats/JSON/json_ast_builder.rb +18 -19
- data/examples/data_formats/JSON/json_ast_nodes.rb +9 -12
- data/examples/data_formats/JSON/json_demo.rb +1 -1
- data/examples/general/calc_iter1/calc_ast_builder.rb +27 -30
- data/examples/general/calc_iter1/calc_ast_nodes.rb +8 -17
- data/examples/general/calc_iter1/calc_lexer.rb +4 -5
- data/examples/general/calc_iter1/spec/calculator_spec.rb +0 -2
- data/lib/rley/constants.rb +1 -1
- data/lib/rley/gfg/grm_flow_graph.rb +1 -1
- data/lib/rley/parser/cst_builder.rb +2 -3
- data/lib/rley/parser/parse_forest_factory.rb +1 -2
- data/lib/rley/parser/parse_tree_builder.rb +16 -19
- data/lib/rley/parser/parse_tree_factory.rb +0 -1
- data/lib/rley/parser/parse_walker_factory.rb +1 -1
- data/lib/rley/syntax/grammar.rb +17 -15
- data/spec/rley/gfg/grm_flow_graph_spec.rb +1 -1
- data/spec/rley/parser/ast_builder_spec.rb +29 -29
- data/spec/rley/parser/cst_builder_spec.rb +3 -3
- data/spec/rley/parser/error_reason_spec.rb +4 -4
- data/spec/rley/support/grammar_arr_int_helper.rb +3 -3
- data/spec/spec_helper.rb +6 -5
- metadata +2 -8
- data/examples/general/calc_iter2/calc_ast_builder.rb +0 -186
- data/examples/general/calc_iter2/calc_ast_nodes.rb +0 -151
- data/examples/general/calc_iter2/calc_demo.rb +0 -40
- data/examples/general/calc_iter2/calc_grammar.rb +0 -28
- data/examples/general/calc_iter2/calc_lexer.rb +0 -81
- data/examples/general/calc_iter2/calc_parser.rb +0 -24
@@ -1,81 +0,0 @@
|
|
1
|
-
# File: calc_lexer.rb
|
2
|
-
# Lexer for a basic arithmetical expression parser
|
3
|
-
require 'strscan'
|
4
|
-
require 'rley' # Load the gem
|
5
|
-
|
6
|
-
|
7
|
-
class CalcLexer
|
8
|
-
attr_reader(:scanner)
|
9
|
-
attr_reader(:lineno)
|
10
|
-
attr_reader(:line_start)
|
11
|
-
attr_reader(:name2symbol)
|
12
|
-
|
13
|
-
@@lexeme2name = {
|
14
|
-
'(' => 'LPAREN',
|
15
|
-
')' => 'RPAREN',
|
16
|
-
'+' => 'PLUS',
|
17
|
-
'-' => 'MINUS',
|
18
|
-
'*' => 'STAR',
|
19
|
-
'/' => 'DIVIDE',
|
20
|
-
'**' => 'POWER'
|
21
|
-
}.freeze
|
22
|
-
|
23
|
-
class ScanError < StandardError; end
|
24
|
-
|
25
|
-
def initialize(source, aGrammar)
|
26
|
-
@scanner = StringScanner.new(source)
|
27
|
-
@name2symbol = aGrammar.name2symbol
|
28
|
-
@lineno = 1
|
29
|
-
end
|
30
|
-
|
31
|
-
def tokens()
|
32
|
-
tok_sequence = []
|
33
|
-
until @scanner.eos?
|
34
|
-
token = _next_token
|
35
|
-
tok_sequence << token unless token.nil?
|
36
|
-
end
|
37
|
-
|
38
|
-
return tok_sequence
|
39
|
-
end
|
40
|
-
|
41
|
-
private
|
42
|
-
|
43
|
-
def _next_token()
|
44
|
-
skip_whitespaces
|
45
|
-
curr_ch = scanner.peek(1)
|
46
|
-
return nil if curr_ch.nil?
|
47
|
-
|
48
|
-
token = nil
|
49
|
-
|
50
|
-
if '()+/'.include? curr_ch
|
51
|
-
# Single character token
|
52
|
-
token = build_token(@@lexeme2name[curr_ch], scanner.getch)
|
53
|
-
|
54
|
-
elsif lexeme = scanner.scan(/\*\*/)
|
55
|
-
token = build_token(@@lexeme2name[lexeme], lexeme)
|
56
|
-
elsif lexeme = scanner.scan(/\*/)
|
57
|
-
token = build_token(@@lexeme2name[lexeme], lexeme)
|
58
|
-
elsif lexeme = scanner.scan(/-?[0-9]+(\.[0-9]+)?([eE][-+]?[0-9])?/)
|
59
|
-
token = build_token('NUMBER', lexeme)
|
60
|
-
elsif lexeme = scanner.scan(/-/)
|
61
|
-
token = build_token(@@lexeme2name[curr_ch], lexeme)
|
62
|
-
else # Unknown token
|
63
|
-
erroneous = curr_ch.nil? ? '' : curr_ch
|
64
|
-
sequel = scanner.scan(/.{1,20}/)
|
65
|
-
erroneous += sequel unless sequel.nil?
|
66
|
-
raise ScanError.new("Unknown token #{erroneous}")
|
67
|
-
end
|
68
|
-
|
69
|
-
return token
|
70
|
-
end
|
71
|
-
|
72
|
-
def build_token(aSymbolName, aLexeme)
|
73
|
-
token_type = name2symbol[aSymbolName]
|
74
|
-
return Rley::Tokens::Token.new(aLexeme, token_type)
|
75
|
-
end
|
76
|
-
|
77
|
-
def skip_whitespaces()
|
78
|
-
scanner.scan(/[ \t\f\n\r]+/)
|
79
|
-
end
|
80
|
-
|
81
|
-
end # class
|
@@ -1,24 +0,0 @@
|
|
1
|
-
# Purpose: to demonstrate how to build and render a parse tree for JSON
|
2
|
-
# language
|
3
|
-
require_relative 'calc_lexer'
|
4
|
-
require_relative 'calc_grammar'
|
5
|
-
|
6
|
-
# A parser for arithmetic expressions
|
7
|
-
class CalcParser < Rley::Parser::GFGEarleyParser
|
8
|
-
attr_reader(:source_file)
|
9
|
-
|
10
|
-
# Constructor
|
11
|
-
def initialize()
|
12
|
-
# Builder the Earley parser with the calculator grammar
|
13
|
-
super(CalcGrammar)
|
14
|
-
end
|
15
|
-
|
16
|
-
def parse_expression(aText)
|
17
|
-
lexer = CalcLexer.new(aText, grammar)
|
18
|
-
result = parse(lexer.tokens)
|
19
|
-
|
20
|
-
return result
|
21
|
-
end
|
22
|
-
end # class
|
23
|
-
|
24
|
-
# End of file
|