-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d0082b6
commit 6e0fa2e
Showing
3 changed files
with
62 additions
and
62 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,24 @@ | ||
import AlternativeParser from "../parser/AlternativeParser.js" | ||
import CapturingGroupParser from "../parser/CapturingGroupParser.js" | ||
import ParentChildTransformer from "./ParentChildTransformer.js" | ||
import SequenceParser from "../parser/SequenceParser.js" | ||
import Transformer from "./Transformer.js" | ||
|
||
export default class InlineParsersTransformer extends Transformer { | ||
/** @extends {ParentChildTransformer<[AlternativeParser, SequenceParser], [AlternativeParser, SequenceParser]>} */ | ||
export default class InlineParsersTransformer extends ParentChildTransformer { | ||
|
||
static #None = class { } | ||
constructor() { | ||
super([AlternativeParser, SequenceParser], [AlternativeParser, SequenceParser]) | ||
} | ||
|
||
/** | ||
* @template T | ||
* @param {Parser<T>} parser | ||
* @return {Parser<T>} | ||
* @param {AlternativeParser<Parser<any>[]> | SequenceParser<Parser<any>[]>} parent | ||
* @param {AlternativeParser<Parser<any>[]> | SequenceParser<Parser<any>[]>} child | ||
* @returns {Parser<any>[]} | ||
*/ | ||
doTransform(parser) { | ||
/** | ||
* @type {(new (...args: any) => AlternativeParser<[Parser<any>, ...Parser<any>[]]>) | ||
* | (new (...args: any) => SequenceParser<[Parser<any>, ...Parser<any>[]]>)} | ||
*/ | ||
const type = parser instanceof AlternativeParser ? AlternativeParser : SequenceParser | ||
let changed = false | ||
/** @type {Parser<any>[]} */ | ||
let children = parser.unwrap() | ||
if (parser instanceof type) { | ||
for (let i = 0; i < children.length; ++i) { | ||
let current = children[i].actualParser([CapturingGroupParser]) | ||
if (current instanceof type) { | ||
children.splice( | ||
i, | ||
1, | ||
...current.parsers.map(p => children[i].withActualParser(p, [CapturingGroupParser])) | ||
) | ||
changed = true | ||
--i | ||
continue | ||
} | ||
const transformed = this.doTransform(current) | ||
if (current != transformed) { | ||
children[i] = children[i].withActualParser(transformed, [CapturingGroupParser]) | ||
changed = true | ||
} | ||
} | ||
} else { | ||
children = children.map(child => { | ||
const transformed = this.doTransform(child) | ||
changed ||= child !== transformed | ||
return transformed | ||
}) | ||
} | ||
if (changed) { | ||
return parser.wrap(...children) | ||
} | ||
return parser | ||
doTransformChild(parent, child) { | ||
return parent instanceof AlternativeParser && child instanceof AlternativeParser | ||
|| parent instanceof SequenceParser && child instanceof SequenceParser | ||
? child.unwrap() | ||
: null | ||
} | ||
|
||
} |
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
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