Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON rewrite #1029

Merged
merged 9 commits into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Simplify rules
  • Loading branch information
pyrmont committed Jul 12, 2019
commit 5a6295ba968a16665b319d32771044268ee952bb
49 changes: 14 additions & 35 deletions lib/rouge/lexers/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,30 @@ class JSON < RegexLexer
rule %r/{/, Punctuation, :object
rule %r/\[/, Punctuation, :array

rule %r/("[^"]*")(:?)/ do |m|
if m[2] == ":"
groups Name::Label, Punctuation
push :value
else
token Str::Double
end
end

rule(%r//) { push :value }
mixin :name
mixin :value
end

state :object do
mixin :whitespace
rule %r/"/, Name::Label, :name
rule %r/:/, Punctuation, :value
rule %r/,/, Punctuation
mixin :name
mixin :value
rule %r/}/, Punctuation, :pop!
rule %r/,/, Punctuation
end


state :name do
rule %r/("(?:\"|[^"\n])*?")(:)/ do
groups Name::Label, Punctuation
end
end

state :value do
mixin :whitespace
rule %r/"/, Str::Double, :string_value
mixin :constants
rule %r/"/, Str::Double, :string_value
rule %r/\[/, Punctuation, :array
rule %r/{/, Punctuation, :object
rule %r/}/ do
if stack[-2].name == :object
token Punctuation
pop! 2 # pop both this state and the :object one below it
else
token Error
pop!
end
end
rule %r/,/, Punctuation, :pop!
rule %r/:/, Punctuation
end

state :name do
rule %r/[^\\"]+/, Name::Label
rule %r/\\./, Name::Label
rule %r/"/, Name::Label, :pop!
end

state :string_value do
Expand All @@ -73,11 +54,9 @@ class JSON < RegexLexer
end

state :array do
mixin :value
rule %r/\]/, Punctuation, :pop!
rule %r/"/, Str::Double, :string_value
rule %r/,/, Punctuation
mixin :constants
mixin :root
end

state :constants do
Expand Down
7 changes: 7 additions & 0 deletions spec/visual/samples/json
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,10 @@
"hex": "#0F0"
}
}

"this": { "is": "not syntactically correct JSON but is here for backwards compatibility" }
true
100
null
"a": "test"
"string"