Module: Jade::Parsing

Extended by:
Parsing, Combinators::Dsl
Includes:
Combinators, Token, Type
Included in:
Parsing
Defined in:
lib/jade/parsing.rb,
lib/jade/parsing/type.rb,
lib/jade/parsing/error.rb,
lib/jade/parsing/token.rb,
lib/jade/parsing/combinators.rb

Defined Under Namespace

Modules: Combinators, Token, Type Classes: EOFError, Error, FunctionCallPostfix, InvalidOperatorError, KeyedCallPostfix, MemberAccessPostfix, MissingThenError, UnexpectedTokenError

Constant Summary collapse

TOP_LEVEL_SYNC =

Tokens that start a top-level declaration. The recovering parser skips to one of these (or EOF) when it can’t make progress in tolerant mode.

%i[def type struct import uses implements interface module].freeze

Constants included from Type

Type::CONSTRUCTORS

Instance Method Summary collapse

Methods included from Combinators::Dsl

parser

Methods included from Type

#anonymous_record, #bool, #constraint, #constructor, #float, #from_symbol, #function, #int, #list, #signature, #string, #unit, #var

Methods included from Combinators

#at_least_one, #comma_sequence, #grouped, #lazy, #many, #maybe, #optional, #recovering_sequence, #recovering_step, #sequence, #skip, #type

Instance Method Details

#body_after_optional_then(state) ⇒ Object



232
233
234
# File 'lib/jade/parsing.rb', line 232

def body_after_optional_then(state)
  state.current&.type == :then ? body.call(state.advance) : body.call(state)
end

#checked_branch_body(in_tok) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/jade/parsing.rb', line 236

def checked_branch_body(in_tok)
  P.new do |state|
    next body.call(state.advance) if state.current&.type == :then

    next_tok = state.current
    if next_tok && state.same_line?(in_tok.range.begin, next_tok.range.begin)
      next Err[[
        MissingThenError.new(
          entry:    state.entry,
          span:     next_tok.range,
          actual:   next_tok,
          expected: :then,
          committed: true,
        ),
        state,
      ]]
    end

    body.call(state)
  end
end

#parse(tokens, source:, parser: program, tolerant: false) ⇒ Object



38
39
40
41
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
70
71
72
73
74
75
76
77
78
79
# File 'lib/jade/parsing.rb', line 38

def parse(tokens, source:, parser: program, tolerant: false)
  comments = tokens.select { it.type == :comment }
  state = State.new(
    tokens:    tokens.reject { it.type == :comment },
    entry:    source.uri,
    tolerant:,
    source:,
  )

  result = parser.call(state)

  if tolerant
    # Tolerant mode: always Ok. Returns (ast, comments, diagnostics).
    case result
    in Ok([ast, end_state])
      Ok[[ast, comments, end_state.diagnostics]]

    in Err([err, err_state])
      diagnostics = err_state.diagnostics.add(error_to_diagnostic(err))
      Ok[[nil, comments, diagnostics]]
    end
  else
    result
      .and_then { |(ast, end_state)|
        end_state.eof? ?
          Ok[[ast, end_state]] :
          end_state.current.then { |tok|
            Err[[
              UnexpectedTokenError.new(
                entry:    end_state.entry,
                span:     tok.range,
                actual:   tok,
                expected: "end of input",
              ),
              end_state,
            ]]
          }
      }
      .map { [it.first, comments] }
      .map_error(&:first)
  end
end