Module: Kumi::Parser::TextParser

Defined in:
lib/kumi/parser/text_parser.rb

Overview

The public entry point. kumi-core calls ‘TextParser.parse(src, source_file:)` and nothing else; `valid?` and `validate` exist for editor/tooling use.

Parse errors are raised as Kumi::Errors::SyntaxError carrying both a self-contained, framed message (so standalone callers get a useful string) and a Location object (so kumi-core’s frontend can render its own frame without re-parsing the message). The message itself is the bare what/why, without location — the frame and ‘file:line:col` header are added by the presentation layer from the Location.

Class Method Summary collapse

Class Method Details

.parse(text, source_file: 'schema') ⇒ Object



17
18
19
20
21
22
23
# File 'lib/kumi/parser/text_parser.rb', line 17

def parse(text, source_file: 'schema')
  source = Source.new(text, file: source_file)
  tokens = Lexer.new(source).tokenize
  Parser.new(tokens, source).parse
rescue ParseError => e
  raise Kumi::Errors::SyntaxError.new(e.short_message, e.location)
end

.valid?(text, source_file: 'schema') ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
# File 'lib/kumi/parser/text_parser.rb', line 25

def valid?(text, source_file: 'schema')
  parse(text, source_file: source_file)
  true
rescue Kumi::Errors::SyntaxError
  false
end

.validate(text, source_file: 'schema') ⇒ Object

Returns an array of diagnostic hashes (empty when valid) for editors.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kumi/parser/text_parser.rb', line 33

def validate(text, source_file: 'schema')
  source = Source.new(text, file: source_file)
  tokens = Lexer.new(source).tokenize
  Parser.new(tokens, source).parse
  []
rescue ParseError => e
  [{
    line: e.line,
    column: e.column,
    message: e.short_message,
    severity: :error,
    type: :syntax
  }]
end