Class: Kumi::Parser::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/kumi/parser/base.rb

Overview

Text parser using tokenizer + direct AST construction

Class Method Summary collapse

Class Method Details

.create_diagnostic(error, source_file) ⇒ Object



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

def self.create_diagnostic(error, source_file)
  location = if error.is_a?(Errors::ParseError) && error.token
               error.token.location
             elsif error.respond_to?(:location)
               error.location
             else
               nil
             end

  {
    line: location&.line || 1,
    column: location&.column || 1,
    message: error.message,
    severity: :error,
    type: :syntax
  }
end

.parse(source, source_file: '<input>') ⇒ Object



11
12
13
14
# File 'lib/kumi/parser/base.rb', line 11

def self.parse(source, source_file: '<input>')
  tokens = SmartTokenizer.new(source, source_file: source_file).tokenize
  Kumi::Parser::DirectParser.new(tokens).parse
end

.valid?(source, source_file: '<input>') ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
# File 'lib/kumi/parser/base.rb', line 16

def self.valid?(source, source_file: '<input>')
  parse(source, source_file: source_file)
  true
rescue Errors::TokenizerError, Errors::ParseError
  false
end

.validate(source, source_file: '<input>') ⇒ Object



23
24
25
26
27
28
# File 'lib/kumi/parser/base.rb', line 23

def self.validate(source, source_file: '<input>')
  parse(source, source_file: source_file)
  []
rescue Errors::TokenizerError, Errors::ParseError => e
  [create_diagnostic(e, source_file)]
end