Class: ABNF::Parser::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/abnf/parser/compiler.rb,
lib/abnf/parser/compiler/group.rb,
lib/abnf/parser/compiler/token.rb,
lib/abnf/parser/compiler/option.rb,
lib/abnf/parser/compiler/element.rb,
lib/abnf/parser/compiler/rule_list.rb,
lib/abnf/parser/compiler/tokenizer.rb,
lib/abnf/parser/compiler/repetition.rb,
lib/abnf/parser/compiler/alternation.rb,
lib/abnf/parser/compiler/rule_builder.rb,
lib/abnf/parser/compiler/concatenation.rb

Defined Under Namespace

Classes: Alternation, Concatenation, Element, Frame, Group, Option, Repetition, RuleBuilder, RuleList, Token, Tokenizer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens, rule_list = nil) ⇒ Compiler

Returns a new instance of Compiler.



7
8
9
10
# File 'lib/abnf/parser/compiler.rb', line 7

def initialize tokens, rule_list=nil
  @tokens = tokens
  @rule_list = rule_list
end

Instance Attribute Details

#outputObject

Returns the value of attribute output.



4
5
6
# File 'lib/abnf/parser/compiler.rb', line 4

def output
  @output
end

#tokensObject (readonly)

Returns the value of attribute tokens.



5
6
7
# File 'lib/abnf/parser/compiler.rb', line 5

def tokens
  @tokens
end

Class Method Details

.build(abnf, include_common: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/abnf/parser/compiler.rb', line 12

def self.build abnf, include_common: nil
  include_common ||= false

  if include_common
    common_rule_list = Parser::RuleList.common
  end

  tokenizer = Tokenizer.build abnf
  instance = new tokenizer, common_rule_list
  instance.push RuleList
  instance
end

.call(*arguments) ⇒ Object



25
26
27
28
29
# File 'lib/abnf/parser/compiler.rb', line 25

def self.call *arguments
  instance = build *arguments
  instance.()
  instance.rule_list
end

Instance Method Details

#callObject



31
32
33
34
35
36
37
# File 'lib/abnf/parser/compiler.rb', line 31

def call
  tokens.each do |token|
    handle token
  end

  output
end

#current_frameObject



39
40
41
# File 'lib/abnf/parser/compiler.rb', line 39

def current_frame
  stack.last
end

#handle(token) ⇒ Object



43
44
45
46
# File 'lib/abnf/parser/compiler.rb', line 43

def handle token
  return unless current_frame
  current_frame.(token)
end

#pop(rule) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/abnf/parser/compiler.rb', line 48

def pop rule
  frame = stack.pop

  self.output = rule if stack.empty?

  frame.exited rule
end

#push(rule_builder_cls, &block) ⇒ Object



56
57
58
59
60
# File 'lib/abnf/parser/compiler.rb', line 56

def push rule_builder_cls, &block
  rule_builder = rule_builder_cls.new self
  frame = Frame.new rule_builder, block
  stack.push frame
end

#rootObject



62
63
64
# File 'lib/abnf/parser/compiler.rb', line 62

def root
  stack.first
end

#rule_listObject



66
67
68
# File 'lib/abnf/parser/compiler.rb', line 66

def rule_list
  @rule_list ||= Parser::RuleList.new
end

#stackObject



70
71
72
# File 'lib/abnf/parser/compiler.rb', line 70

def stack
  @stack ||= []
end