Class: Archsight::Query::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/archsight/query/parser.rb

Overview

Recursive descent parser for the architecture query language.

Instance Method Summary collapse

Constructor Details

#initialize(tokens) ⇒ Parser

Returns a new instance of Parser.



8
9
10
11
# File 'lib/archsight/query/parser.rb', line 8

def initialize(tokens)
  @tokens = tokens
  @position = 0
end

Instance Method Details

#parseObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/archsight/query/parser.rb', line 13

def parse
  # Check for kind filter prefix: "Kind: expression"
  kind_filter = nil
  # Check it's a valid Kind (starts with capital letter)
  if current_token.type == :IDENTIFIER && peek_token&.type == :COLON && (current_token.value =~ /^[A-Z]/)
    kind_filter = current_token.value
    advance # consume identifier
    advance # consume colon
  end

  # Expression is optional when kind filter is present
  # "TechnologyArtifact:" is valid and returns all resources of that kind
  expression = if current_token.type == :EOF
                 nil
               else
                 parse_or_expression
               end

  # Empty query (no kind filter and no expression) is an error
  if kind_filter.nil? && expression.nil?
    raise Archsight::Query::ParseError.new(
      "Empty query: expected kind filter or expression",
      position: 0,
      source: nil
    )
  end

  expect(:EOF)

  Archsight::Query::AST::QueryNode.new(kind_filter, expression)
end