Skip to content

Commit

Permalink
Move entry point to main include
Browse files Browse the repository at this point in the history
  • Loading branch information
frodrigo committed Apr 14, 2024
1 parent 2e4f8c3 commit ed506d8
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 14 deletions.
20 changes: 20 additions & 0 deletions lib/overpass_parser.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
# frozen_string_literal: true
# typed: strict

# load the native extension
require File.expand_path(File.join('..', 'ext', 'overpass_parser', 'overpass_parser'), __dir__)
require 'sorbet-runtime'
require_relative('overpass_parser/visitor')

module OverpassParser
extend T::Sig

sig do
params(
overpass_script: String
).returns(Nodes::Request)
end
def self.parse(overpass_script)
parser = OverpassParser::Parser.parse(overpass_script)
parser.add_error_listener(ErrorListener.new)
walker = OverpassParser::Walker.new
parser.visit(walker)
walker.stack
end
end
20 changes: 11 additions & 9 deletions lib/overpass_parser/visitor.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true
# typed: true

require 'overpass_parser'
require 'sorbet-runtime'
require_relative 'nodes/selectors'
require_relative 'nodes/filters'
Expand All @@ -12,9 +11,20 @@
require_relative 'nodes/out'

module OverpassParser
extend T::Sig

class ParsingError < RuntimeError; end

class ErrorListener < OverpassParser::BaseErrorListener
extend T::Sig

sig do
params(
line: Integer,
char_position_in_line: Integer,
msg: String
).void
end
def syntax_error(line, char_position_in_line, msg)
raise(ParsingError, "line #{line}:#{char_position_in_line} #{msg}")
end
Expand Down Expand Up @@ -127,12 +137,4 @@ def visit_token(ctx)
@stack.push(text)
end
end

def self.parse(overpass_script)
parser = OverpassParser::Parser.parse(overpass_script)
parser.add_error_listener(ErrorListener.new)
walker = OverpassParser::Walker.new
parser.visit(walker)
walker.stack
end
end
2 changes: 1 addition & 1 deletion test/overpass_parser/filters_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TestFilters < Minitest::Test
end
def parse(query)
tree = OverpassParser.parse("node#{query};")
tree.queries[0].filters
T.must(T.cast(tree.queries[0], T.nilable(QueryObjects))&.filters)
end

sig { void }
Expand Down
2 changes: 1 addition & 1 deletion test/overpass_parser/query_objects_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TestQueryObjects < Minitest::Test
end
def parse(query)
tree = OverpassParser.parse("#{query};")
tree.queries[0]
T.must(T.cast(tree.queries[0], QueryObjects))
end

sig { void }
Expand Down
2 changes: 1 addition & 1 deletion test/overpass_parser/selectors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TestSelectors < Minitest::Test
end
def parse(query)
tree = OverpassParser.parse("node#{query};")
tree.queries[0].selectors
T.must(T.cast(tree.queries[0], T.nilable(QueryObjects))&.selectors)
end

sig { void }
Expand Down
2 changes: 0 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
require 'overpass_parser'
require 'overpass_parser/visitor'
require 'overpass_parser/sql_dialect/sql_dialect'
require 'overpass_parser/sql_dialect/postgres'

require 'minitest/autorun'

0 comments on commit ed506d8

Please sign in to comment.