Skip to content

Commit

Permalink
🎨 export non-dumping json parser
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAfroOfDoom committed May 14, 2024
1 parent 74a6855 commit 41b5c8b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/java-edition/src/mcfunction/parser/argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ function block(isPredicate: boolean): core.InfallibleParser<BlockNode> {
const blockState: core.InfallibleParser<BlockNode> = block(false)
export const blockPredicate: core.InfallibleParser<BlockNode> = block(true)

export const component = json.parser.json()
export const component = json.parser.json

function double(
min = DoubleMin,
Expand Down
2 changes: 1 addition & 1 deletion packages/json/src/parser/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { json } from './entry.js'
export const array: core.Parser<JsonArrayNode> = (src, ctx) => {
const parser = core.list({
start: '[',
value: json(),
value: json,
sep: ',',
trailingSep: false,
end: ']',
Expand Down
19 changes: 17 additions & 2 deletions packages/json/src/parser/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const LegalNumberStart = new Set([
'9',
'-',
])
export function json(dumpErrors = false): core.Parser<JsonNode> {
function jsonParser(dumpErrors: boolean): core.Parser<JsonNode> {
return (src, ctx) => {
const result = core.select([
{ predicate: (src) => src.tryPeek('['), parser: array },
Expand All @@ -44,4 +44,19 @@ export function json(dumpErrors = false): core.Parser<JsonNode> {
}
}

export const entry: core.Parser<JsonNode> = json(true)
/**
* A JSON parser that dumps any parser errors after it finishes parsing.
* This should be used when it is the root parser, e.g. for this package's
* initialization method (the JSON package).
*/
export const entry = jsonParser(true)

/**
* A JSON parser that doesn't dump parser errors after it finishes parsing.
* This should be used when it is a child parser under another parent parser,
* e.g. in the JSON `array` parser.
*
* Since this parser doesn't dump its errors when it's done, those errors
* should be subsequently absorbed by the parent parser's `ParserContext`.
*/
export const json = jsonParser(false)
2 changes: 1 addition & 1 deletion packages/json/src/parser/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const object: core.InfallibleParser<JsonObjectNode> = (src, ctx) => {
pair: {
key: string,
sep: ':',
value: json(),
value: json,
end: ',',
trailingEnd: false,
},
Expand Down

0 comments on commit 41b5c8b

Please sign in to comment.