Skip to content
Snippets Groups Projects
Commit 077bcd7b authored by Jeanine Adkisson's avatar Jeanine Adkisson
Browse files

Merge pull request #244 from keenlogics/feature-json-comment-and-ellipsis2

Add comment and ellipsis support to json lexer.
parents a8a81b7e 18a27860
No related branches found
No related tags found
No related merge requests found
{ "one": 1, "two": 2, "null": null, "simple": true } // a simple json object
Loading
Loading
@@ -219,12 +219,8 @@ module Rouge
 
state :root do
mixin :whitespace
# special case for empty objects
rule /(\{)(\s*)(\})/m do
groups Punctuation, Text::Whitespace, Punctuation
end
rule /(?:true|false|null)\b/, Keyword::Constant
rule /{/, Punctuation, :object_key
rule /{/, Punctuation, :object_key_initial
rule /\[/, Punctuation, :array
rule /-?(?:0|[1-9]\d*)\.\d+(?:e[+-]\d+)?/i, Num::Float
rule /-?(?:0|[1-9]\d*)(?:e[+-]\d+)?/i, Num::Integer
Loading
Loading
@@ -239,6 +235,17 @@ module Rouge
rule string, Str::Double
end
 
# in object_key_initial it's allowed to immediately close the object again
state :object_key_initial do
mixin :whitespace
rule string do
token Name::Tag
goto :object_key
end
rule /}/, Punctuation, :pop!
end
# in object_key at least one more name/value pair is required
state :object_key do
mixin :whitespace
rule string, Name::Tag
Loading
Loading
@@ -258,5 +265,33 @@ module Rouge
mixin :root
end
end
class JSONDOC < JSON
desc "JavaScript Object Notation with extenstions for documentation"
tag 'json-doc'
prepend :root do
mixin :comments
rule /(\.\.\.)/, Comment::Single
end
prepend :object_key_initial do
mixin :comments
rule /(\.\.\.)/, Comment::Single
end
prepend :object_key do
mixin :comments
rule /(\.\.\.)/ do
token Comment::Single
goto :object_key_initial
end
end
state :comments do
rule %r(//.*?$), Comment::Single
end
end
end
end
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021",
"country": "US"
},
"ellipsis": {
"object": { ... },
...
"list": [ ... ],
"locations": {
...
"city": "New York",
...
"postalCode": "10021",
...
},
"key": ...,
...
},
"comments": { // comments
"key": "value", // in
"list": [
// most
], // locations
"key": "value",
"emptyObject": { // comment
}
},
...
"errors": {
"object": { "a", "b" }
},
"emptyObject": {} // comment
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment