Module: Peg::Parsers::CommonParsers

Includes:
Combinators
Included in:
Peg::Parsers, Peg::Parsers
Defined in:
lib/peg.backup/parsers/common_parsers.rb

Instance Method Summary collapse

Methods included from Combinators

#lookahead, #many, #map, #map_result, #maybe, #satisfy, #select, #sequence

Methods included from Combinators::Implementation

#_debug, #_lookahead, #_many, #_map_result, #_satisfy, #_select, #_sequence

Instance Method Details

#id_parser(name: nil, lead_class: :alpha, inner_class: [:alnum, "_"]) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/peg.backup/parsers/common_parsers.rb', line 9

def id_parser(name: nil, lead_class: :alpha, inner_class: [:alnum, "_"] )
  sequence(char_class_parser(*Array(lead_class)),
           many(char_class_parser(*Array(inner_class)), name:),
            name:
          ).map { 
            it.flatten.join.to_sym 
          }
end

#int_parser(name = nil) ⇒ Object

Just parses any string starting with either a ‘+` or `-` sign followed by at least one _decimal digit_.

N.B. that leading zeroes are parsed (and therefore ignored) and will not parse it as a hexadecimal or octal number



24
25
26
27
28
29
30
# File 'lib/peg.backup/parsers/common_parsers.rb', line 24

def int_parser(name=nil)
  parser = sequence(
    maybe(char_parser("+-")),
    many(char_class_parser(:digit), min: 1),
    name: name || "int_parser")
  map_result(parser) { _make_int(it) }
end

#literal_set(set, name: nil, lead_class: :alpha, inner_class: [:alnum, "_"]) ⇒ Object



32
33
34
35
# File 'lib/peg.backup/parsers/common_parsers.rb', line 32

def literal_set(set, name: nil, lead_class: :alpha, inner_class: [:alnum, "_"])
  id_parser(name:, lead_class:, inner_class:)
  .satisfy { set.member? it }
end

#ws_parser(name = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/peg.backup/parsers/common_parsers.rb', line 37

def ws_parser(name=nil)
  map(
         many(
           char_class_parser(:space),
           min: 1,
           name: "ws_parser"
         )
  ) {|_| nil}
end