Skip to content

Commit

Permalink
Support Cypher multi-line comments (#1710)
Browse files Browse the repository at this point in the history
* resolves #1709 support Cypher multi-line comments

* Remove empty line

* Add newline at the end of the file
  • Loading branch information
ggrossetie committed Jan 23, 2022
1 parent b3ee7ac commit c39c98a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/rouge/lexers/cypher.rb
Expand Up @@ -45,6 +45,7 @@ def self.keywords
state :root do
rule %r/[\s]+/, Text
rule %r(//.*?$), Comment::Single
rule %r(/\*), Comment::Multiline, :multiline_comments

rule %r([*+\-<>=&|~%^]), Operator
rule %r/[{}),;\[\]]/, Str::Symbol
Expand Down Expand Up @@ -103,6 +104,13 @@ def self.keywords
rule %r/'(\\\\|\\'|[^'])*'/, Str::Single
rule %r/`(\\\\|\\`|[^`])*`/, Str::Backtick
end

state :multiline_comments do
rule %r(/[*]), Comment::Multiline, :multiline_comments
rule %r([*]/), Comment::Multiline, :pop!
rule %r([^/*]+), Comment::Multiline
rule %r([/*]), Comment::Multiline
end
end
end
end
20 changes: 20 additions & 0 deletions spec/lexers/cypher_spec.rb
Expand Up @@ -15,4 +15,24 @@
assert_guess :mimetype => 'application/x-cypher-query'
end
end

describe 'lexing' do
include Support::Lexing

describe 'comments' do
describe 'inline comment' do
it 'handles inline comment' do
assert_tokens_includes "RETURN point({longitude: 2.2105491 /* x */, latitude: 48.9250016 /* y */, srid: 4326}) AS home",
["Comment.Multiline", "/* x */"],
["Comment.Multiline", "/* y */"]
end

it 'handles multiline comment' do
assert_tokens_includes "RETURN /* drum\nrolls */ 42",
["Comment.Multiline", "/* drum\nrolls */"],
["Literal.Number.Integer", "42"]
end
end
end
end
end
11 changes: 11 additions & 0 deletions spec/support/lexing.rb
Expand Up @@ -43,5 +43,16 @@ def assert_tokens_equal(text, *expected)
actual = lexer.lex(text).map { |token, value| [ token.qualname, value ] }
assert { expected == actual }
end

def assert_tokens_includes(text, *expected)
if expected.first.is_a? Rouge::Lexer
lexer = expected.shift
else
lexer = subject
end

actual = lexer.lex(text).map { |token, value| [ token.qualname, value ] }
expected.all? { |e| assert_includes actual, e }
end
end
end
2 changes: 2 additions & 0 deletions spec/visual/samples/cypher
Expand Up @@ -100,3 +100,5 @@ MATCH (p:Person)-[rel]->(:Movie {title:'The Matrix'})
RETURN p.name, type(rel)

RETURN sign(-17), sign(0.1), sign(0xABCDEF), sign(000)

RETURN point({longitude: 2.2105491 /* x */, latitude: 48.9250016 /* y */, srid: 4326}) AS home

0 comments on commit c39c98a

Please sign in to comment.