Module: Jade::Parsing::Combinators

Extended by:
Dsl
Included in:
Jade::Parsing, Symbol::Parser
Defined in:
lib/jade/parsing/combinators.rb

Defined Under Namespace

Modules: Dsl Classes: CommaList, P, State

Instance Method Summary collapse

Methods included from Dsl

parser

Instance Method Details

#at_least_one(parser, separated_by: none.skip) ⇒ Object



25
26
27
28
# File 'lib/jade/parsing/combinators.rb', line 25

def at_least_one(parser, separated_by: none.skip)
  tail = separated_by >> sequence(parser, separated_by:)
  parser >> optional(tail, default: [])
end

#comma_sequence(parser) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/jade/parsing/combinators.rb', line 53

def comma_sequence(parser)
  inner = sequence(parser, separated_by: type(:comma).skip)
  P.new do |state|
    inner.call(state).and_then do |(items, state1)|
      trailing, state2 =
        if !state1.eof? && state1.current.type == :comma
          [true, state1.advance]
        else
          [false, state1]
        end

      Ok[[CommaList.new(items:, trailing_comma: trailing), state2]]
    end
  end
end

#grouped(parser) ⇒ Object



21
22
23
# File 'lib/jade/parsing/combinators.rb', line 21

def grouped(parser)
  type(:lparen).skip >> parser >> type(:rparen).skip
end

#lazy(&block) ⇒ Object



169
170
171
172
173
# File 'lib/jade/parsing/combinators.rb', line 169

def lazy(&block)
  P.new do |input|
    block.call.call(input)
  end
end

#many(parser) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/jade/parsing/combinators.rb', line 107

def many(parser)
  P.new do |state|
    oks = []
    current = state
    committed_err = nil

    loop do
      break if current.eof?

      case parser.call(current)
      in Ok([value, next_state])
        oks << value
        current = next_state
      in Err([err, err_state])
        committed_err = Err[[err, err_state]] if err.committed?
        break
      end
    end

    committed_err || Ok[[oks, current]]
  end
end

#maybe(parser) ⇒ Object

Consume ‘parser` if it matches; if not, drop the slot from the surrounding `>>` tuple (no value, no failure).



32
33
34
# File 'lib/jade/parsing/combinators.rb', line 32

def maybe(parser)
  (parser | none).skip
end

#optional(parser, default: nil) ⇒ Object

Consume ‘parser` if it matches; otherwise inject `default` as the value. Keeps tuple arity stable in `>>` chains.



38
39
40
# File 'lib/jade/parsing/combinators.rb', line 38

def optional(parser, default: nil)
  parser | none.map { default }
end

#recovering_sequence(parser, sync_types:) ⇒ Object

Tolerant counterpart of ‘sequence`. Strict mode (the default) behaves identically; tolerant mode records a diagnostic and resumes at the next sync token instead of failing.



77
78
79
# File 'lib/jade/parsing/combinators.rb', line 77

def recovering_sequence(parser, sync_types:)
  P.new { recovering_step(parser, sync_types, it, []) }
end

#recovering_step(parser, sync_types, state, results) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/jade/parsing/combinators.rb', line 81

def recovering_step(parser, sync_types, state, results)
  return Ok[[results, state]] if state.eof?

  case parser.call(state)
  in Ok([value, next_state])
    recovering_step(parser, sync_types, next_state, [*results, value])

  in Err([err, err_state]) unless state.tolerant
    err.committed? ? Err[[err, err_state]] : Ok[[results, state]]

  in Err([err, err_state])
    # Always advance at least one token so termination is guaranteed
    # even when sync_types isn't ahead.
    moved_past = err.committed? && err_state.position > state.position
    recovered = (moved_past ? err_state : state.advance)
      .skip_until(sync_types)
      .add_diagnostic(err.to_diagnostic)

    recovering_step(parser, sync_types, recovered, results)
  end
end

#sequence(parser, separated_by: none.skip) ⇒ Object



42
43
44
45
# File 'lib/jade/parsing/combinators.rb', line 42

def sequence(parser, separated_by: none.skip)
  (parser.map { [it] } >> many(separated_by >> parser))
    .map { it.flatten(1) }
end

#skip(parser) ⇒ Object



103
104
105
# File 'lib/jade/parsing/combinators.rb', line 103

def skip(parser)
  parser.map { |_| :skip }
end

#type(type) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/jade/parsing/combinators.rb', line 130

def type(type)
  (@types ||= {})[type] ||= P.new do |state|
    if state.eof?
      Err[[
        Parsing::EOFError.new(
          entry:    state.entry,
          span:     nil,
          expected: type,
        ),
        state,
      ]]

    elsif state.current.type == :invalid_op
      Err[[
        Parsing::InvalidOperatorError.new(
          entry:    state.entry,
          span:     state.current.range,
          actual:   state.current,
        ),
        state,
      ]]

    elsif state.current.type == type
      Ok[([state.current, state.advance])]

    else
      Err[[
        Parsing::UnexpectedTokenError.new(
          entry:    state.entry,
          span:     state.current.range,
          actual:   state.current,
          expected: type,
        ),
        state,
      ]]
    end
  end
end