Skip to content

Commit

Permalink
Improve handling of quoted forms in Janet lexer (#1586)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pyrmont authored Oct 1, 2020
1 parent 82f3fcd commit ee713d3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/rouge/lexers/janet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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

Expand All @@ -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

Expand Down
2 changes: 2 additions & 0 deletions spec/visual/samples/janet
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ee713d3

Please sign in to comment.