Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
papa-carlo/src/main/scala/name.lakhin.eliah.projects/papacarlo/examples/Json.scala
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
171 lines (142 sloc)
4.19 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright 2013 Ilya Lakhin (Илья Александрович Лахин) | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
*/ | |
package name.lakhin.eliah.projects | |
package papacarlo.examples | |
import name.lakhin.eliah.projects.papacarlo.lexis.{ | |
Token, | |
Matcher, | |
Tokenizer, | |
Contextualizer | |
} | |
import name.lakhin.eliah.projects.papacarlo.{Syntax, Lexer} | |
import name.lakhin.eliah.projects.papacarlo.syntax.Rule | |
import name.lakhin.eliah.projects.papacarlo.syntax.rules.NamedRule | |
object Json { | |
private def tokenizer = { | |
val tokenizer = new Tokenizer() | |
import tokenizer._ | |
import Matcher._ | |
tokenCategory( | |
"whitespace", | |
oneOrMore(anyOf(" \t\f\n")) | |
).skip | |
tokenCategory( | |
"string", | |
sequence( | |
chunk("\""), | |
zeroOrMore( | |
choice( | |
anyExceptOf("\n\r\\\""), | |
sequence(chunk("\\"), anyOf("\"\\/bfnrt")), | |
sequence( | |
chunk("\\u"), | |
repeat( | |
choice(rangeOf('a', 'f'), rangeOf('A', 'F'), rangeOf('0', '9')), | |
times = 4 | |
) | |
) | |
)), | |
chunk("\"") | |
) | |
) | |
tokenCategory( | |
"number", | |
sequence( | |
optional(chunk("-")), | |
choice( | |
chunk("0"), | |
sequence(rangeOf('1', '9'), zeroOrMore(rangeOf('0', '9'))) | |
), | |
optional(sequence(chunk("."), oneOrMore(rangeOf('0', '9')))), | |
optional( | |
sequence( | |
anyOf("eE"), | |
optional(anyOf("+-")), | |
oneOrMore(rangeOf('0', '9')) | |
)) | |
) | |
) | |
tokenCategory( | |
"alphanum", | |
oneOrMore(rangeOf('a', 'z')) | |
) | |
terminals(",", ":", "{", "}", "[", "]", "//", "/*", "*/") | |
keywords("true", "false", "null") | |
tokenizer | |
} | |
private def contextualizer = { | |
val contextualizer = new Contextualizer | |
import contextualizer._ | |
trackContext("[", "]").allowCaching | |
trackContext("{", "}").allowCaching | |
trackContext("//", Token.LineBreakKind).forceSkip.topContext | |
trackContext("/*", "*/").forceSkip.topContext | |
contextualizer | |
} | |
def lexer = new Lexer(tokenizer, contextualizer) | |
def syntax(lexer: Lexer) = | |
new { | |
val syntax = new Syntax(lexer) | |
import syntax._ | |
import Rule._ | |
val jsonObject = rule("object").cachable.main { | |
sequence( | |
token("{"), | |
zeroOrMore( | |
branch("entry", objectEntry), | |
separator = recover(token(","), | |
"object entries must be separated with , sign") | |
), | |
recover(token("}"), "object must end with } sign") | |
) | |
} | |
val objectEntry = rule("entry") { | |
sequence( | |
capture("key", token("string")), | |
token(":"), | |
branch("value", jsonValue) | |
) | |
} | |
val jsonArray = rule("array").cachable { | |
sequence( | |
token("["), | |
zeroOrMore( | |
branch("value", jsonValue), | |
separator = | |
recover(token(","), "array entries must be separated with , sign") | |
), | |
recover(token("]"), "array must end with ] sign") | |
) | |
} | |
val jsonString = rule("string") { | |
capture("value", token("string")) | |
} | |
val jsonNumber = rule("number") { | |
capture("value", token("number")) | |
} | |
val jsonBoolean = rule("boolean") { | |
capture("value", choice(token("true"), token("false"))) | |
} | |
val jsonNull = rule("null") { | |
token("null") | |
} | |
val jsonValue: NamedRule = subrule("value") { | |
choice(jsonString, | |
jsonNumber, | |
jsonObject, | |
jsonArray, | |
jsonBoolean, | |
jsonNull) | |
} | |
}.syntax | |
} |