Scoop API

scoop:scoop.json:0.1.0

src/parser.scoop

Selected production source · src/parser.scoop

1package scoop.json
3import scoop.collections.LinkedHashMap
5internal class JsonParser(text: String) {
6 private val characters: Iterator<Char> = text.iterator()
7 private var current: Option<Char> = characters.next()
8 private var position: Long = 0L
10 internal fun peek(): Option<Char> = current
12 internal fun take(path: String): Char {
13 if (current == None) {
14 throw error(path, "unexpected end of JSON")
15 }
16 val result = current!!
17 current = characters.next()
18 position = position + 1L
19 return result
20 }
22 internal fun error(path: String, message: String): DecodingException {
23 return DecodingException(path, message + " at character " + position.toString())
24 }
26 internal fun accept(character: Char): Boolean {
27 if (current == Some(character)) {
28 take("")
29 return true
30 }
31 return false
32 }
34 internal fun expect(character: Char, path: String) {
35 if (!accept(character)) {
36 throw error(path, "expected " + character.toString())
37 }
38 }
40 private fun whitespace() {
41 while (current != None) {
42 val character = current!!
43 if (character != ' ' && character != '\t' && character != '\n' && character != '\r') {
44 return
45 }
46 take("")
47 }
48 }
50 internal fun parse(): JsonNode {
51 val result = value("")
52 whitespace()
53 if (current != None) {
54 throw error("", "unexpected input after the root value")
55 }
56 return result
57 }
59 private fun keyword(word: String, result: JsonNode, path: String): JsonNode {
60 for (character in word) {
61 expect(character, path)
62 }
63 return result
64 }
66 private fun value(path: String): JsonNode {
67 whitespace()
68 if (current == None) {
69 throw error(path, "expected a JSON value")
70 }
71 return when (current!!) {
72 case 'n' -> keyword("null", JsonNode.Null, path)
73 case 't' -> keyword("true", JsonNode.BooleanValue(true), path)
74 case 'f' -> keyword("false", JsonNode.BooleanValue(false), path)
75 case '"' -> JsonNode.Text(readJsonString(this, path))
76 case '{' -> record(path)
77 case '[' -> sequence(path)
78 else -> JsonNode.Number(readJsonNumber(this, path))
79 }
80 }
82 private fun record(path: String): JsonNode {
83 expect('{', path)
84 val members = LinkedHashMap<String, JsonNode>()
85 whitespace()
86 if (accept('}')) {
87 return JsonNode.Record(members)
88 }
89 var finished: Boolean = false
90 while (!finished) {
91 whitespace()
92 val name = readJsonString(this, path)
93 val child = childPath(path, name)
94 if (members.containsKey(name)) {
95 throw error(child, "duplicate object key")
96 }
97 whitespace()
98 expect(':', child)
99 members.put(name, value(child))
100 whitespace()
101 finished = accept('}')
102 if (!finished) {
103 expect(',', path)
104 }
105 }
106 return JsonNode.Record(members)
107 }
109 private fun sequence(path: String): JsonNode {
110 expect('[', path)
111 val elements = ArrayList<JsonNode>()
112 whitespace()
113 if (accept(']')) {
114 return JsonNode.Sequence(elements)
115 }
116 var finished: Boolean = false
117 while (!finished) {
118 elements.add(value(childPath(path, elements.size.toString())))
119 whitespace()
120 finished = accept(']')
121 if (!finished) {
122 expect(',', path)
123 }
124 }
125 return JsonNode.Sequence(elements)
126 }