From ee713d3822920792f4d0a6549447c105a08e05e2 Mon Sep 17 00:00:00 2001 From: Michael Camilleri Date: Thu, 1 Oct 2020 13:13:37 +0900 Subject: [PATCH] Improve handling of quoted forms in Janet lexer (#1586) This commit makes a number of fixes to the Janet lexer. Specifically, it: - supports quoting structs/tables; - fixes quoting of arrays and tables; and - removes unnecessary backslashes in character classes. --- lib/rouge/lexers/janet.rb | 17 +++++++++-------- spec/visual/samples/janet | 2 ++ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/rouge/lexers/janet.rb b/lib/rouge/lexers/janet.rb index 89f66fb788..18b2465b14 100644 --- a/lib/rouge/lexers/janet.rb +++ b/lib/rouge/lexers/janet.rb @@ -114,7 +114,7 @@ def name_token(name) end end - punctuation = %r/[_!@$%^&*+=~<>.?\/-]/o + punctuation = %r/[_!$%^&*+=~<>.?\/-]/o symbol = %r/([[:alpha:]]|#{punctuation})([[:word:]]|#{punctuation}|:)*/o state :root do @@ -143,20 +143,19 @@ def name_token(name) rule %r/\(/, Punctuation, :function - rule %r/(')([\(\[])/ do + rule %r/(')(@?[(\[{])/ do groups Operator, Punctuation push :quote end - rule %r/(~)([\(\[])/ do + rule %r/(~)(@?[(\[{])/ do groups Operator, Punctuation push :quasiquote end rule %r/[\#~,';\|]/, Operator - rule %r/@?[({\[]/, Punctuation, :push - rule %r/[)}\]]/, Punctuation, :pop! + rule %r/@?[(){}\[\]]/, Punctuation rule symbol, Name end @@ -169,6 +168,8 @@ def name_token(name) end state :function do + rule %r/[\)]/, Punctuation, :pop! + rule symbol do |m| case m[0] when "quote" @@ -187,8 +188,9 @@ def name_token(name) end state :quote do - rule %r/[\(\[]/, Punctuation, :push - rule symbol, Str::Symbol + rule %r/[(\[{]/, Punctuation, :push + rule %r/[)\]}]/, Punctuation, :pop! + rule symbol, Str::Escape mixin :root end @@ -199,7 +201,6 @@ def name_token(name) end rule %r/(\()(\s*)(unquote)(\s+)(\()/ do groups Punctuation, Text, Keyword, Text, Punctuation - push :root push :function end diff --git a/spec/visual/samples/janet b/spec/visual/samples/janet index bb2c5b5407..4b34666981 100644 --- a/spec/visual/samples/janet +++ b/spec/visual/samples/janet @@ -130,6 +130,8 @@ Yet another buffer (def name 'myfunction) (def args '[x y z]) (defn body '[(print x) (print y) (print z)]) +'{:foo (print "bar")} +'@(print 1 2 3) # Quasiquotes ~x