Skip to content

Commit

Permalink
Simplify rules
Browse files Browse the repository at this point in the history
  • Loading branch information
pyrmont committed Jul 12, 2019
1 parent 04238d0 commit 3ed514e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 35 deletions.
49 changes: 14 additions & 35 deletions lib/rouge/lexers/json.rb
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
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"

0 comments on commit 3ed514e

Please sign in to comment.