Class: NanDoc::Filters::CustomTag::TagParser

Inherits:
Object
  • Object
show all
Defined in:
lib/nandoc/filters/tag-parser.rb

Defined Under Namespace

Modules: ParserSymbol Classes: EndSymbolClass, Or, RegexpSymbol, Seq

Constant Summary collapse

EndSymbol =
EndSymbolClass.new

Instance Method Summary collapse

Constructor Details

#initializeTagParser

treetop?



7
8
9
# File 'lib/nandoc/filters/tag-parser.rb', line 7

def initialize
  @last_failure_lines = nil
end

Instance Method Details

#failure_lines(*ignored) ⇒ Object



10
11
12
# File 'lib/nandoc/filters/tag-parser.rb', line 10

def failure_lines *ignored
  @last_failure_lines
end

#get_start_symbolObject



38
39
40
# File 'lib/nandoc/filters/tag-parser.rb', line 38

def get_start_symbol
  get_symbol(:start)
end

#get_symbol(mixed) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/nandoc/filters/tag-parser.rb', line 13

def get_symbol mixed
  if mixed.kind_of?(ParserSymbol)
    mixed
  elsif :start == mixed
    name = symbols[:start]
    sym = get_symbol_with_name(name)
  elsif :end == mixed
    sym = EndSymbol
  elsif mixed.kind_of?(Array)
    sym = Seq.new(self, mixed)
  else
    sym = get_symbol_with_name(mixed)
  end
  sym
end

#get_symbol_with_name(name) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/nandoc/filters/tag-parser.rb', line 28

def get_symbol_with_name name
  thing = symbols[name] or fail("Symbol not found: #{name.inspect}")
  sym = case thing
    when Hash
      RegexpSymbol.new(self, thing, name)
    else
      fail("no: #{thing.inspect}")
  end
  sym
end

#parse(str) ⇒ Object

return tree or (nil and set failure lines)



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/nandoc/filters/tag-parser.rb', line 42

def parse str
  sym = get_start_symbol
  scn = StringScanner.new(str)
  sex = []
  fails = []
  ok = true
  while sym
    ch_ret = sym.parse(scn, sex, fails)
    case ch_ret
    when ParserSymbol
      sym = ch_ret
    when false;
      ok = false
      break
    when true;
      ok = true
      break
    else
      debugger; 'x'
    end
  end
  if ok
    sex
  else
    @last_failure_lines = fails
    nil
  end
end

#symbolsObject



70
71
72
73
74
75
# File 'lib/nandoc/filters/tag-parser.rb', line 70

def symbols
  @symbols ||= begin
    self.class.const_get(:Symbols) or
      fail("Symbols constant not found in #{self.class}")
  end
end