1 # coding: utf-8
2 # frozen_string_literal: true
3
4 require_relative 'grammar.tab'
5
6
7
8 module Umu
9
10 module ConcreteSyntax
11
12 class Parser
13 attr_reader :tokens
14
15
16 def parse(tokens)
17 ASSERT.kind_of tokens, ::Array
18
19 @tokens = tokens.reject { |token|
20 ASSERT.kind_of token, LT::Abstraction::Abstract
21
22 token.separator?
23 }.to_enum
24
25 stmts = do_parse
26 ASSERT.kind_of stmts, ::Array
27 end
28
29
30 def next_token
31 begin
32 token = self.tokens.next
33
34 [token.to_racc_token, token]
35
36 rescue ::StopIteration
37 [false, nil]
38 end
39 end
40
41
42 def on_error(racc_token_id, token, _stack)
43 ASSERT.kind_of racc_token_id, ::Integer
44 ASSERT.opt_kind_of token, LT::Abstraction::Abstract
45 =begin
46 STDERR.printf("Error Token: %s\n",
47 {
48 :racc_token => token_to_str(racc_token_id),
49 :token => token
50 }.inspect
51 )
52 =end
53 raise (
54 if token
55 X::SyntaxError.new(
56 token.loc,
57 "Syntax error near: %s", token.to_s
58 )
59 else
60 X::SyntaxErrorWithoutLocation.new "Syntax error"
61 end
62 )
63 end
64 end
65
66 end # Umu::ConcreteSyntax
67
68 end # Umu