rley 0.5.02 → 0.5.03

Sign up to get free protection for your applications and to get access to all the features.
@@ -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