fix Parser for GDScript

This commit is contained in:
2025-10-30 19:56:44 -05:00
parent ad2f9553d1
commit 829dd9023d
3 changed files with 72 additions and 46 deletions

View File

@@ -1,30 +1,6 @@
class_name Parser
static var symbols: Array[Variant] = [if true:
haxe_ds_StringMap.new().__set("INCLUDE ", Parser.include)
haxe_ds_StringMap.new(), if true:
haxe_ds_StringMap.new().__set("<-", Parser.thread)
haxe_ds_StringMap.new(), if true:
haxe_ds_StringMap.new().__set("->", Parser.divert)
haxe_ds_StringMap.new(), if true:
haxe_ds_StringMap.new().__set("===", Parser.knot)
haxe_ds_StringMap.new(), if true:
haxe_ds_StringMap.new().__set("==", Parser.knot)
haxe_ds_StringMap.new(), if true:
haxe_ds_StringMap.new().__set("=", Parser.stitch)
haxe_ds_StringMap.new(), if true:
haxe_ds_StringMap.new().__set("~", Parser.haxeLine)
haxe_ds_StringMap.new(), if true:
haxe_ds_StringMap.new().__set("```", Parser.haxeBlock)
haxe_ds_StringMap.new(), if true:
haxe_ds_StringMap.new().__set("-", Parser.gather)
haxe_ds_StringMap.new(), if true:
haxe_ds_StringMap.new().__set("*", Parser.choice)
haxe_ds_StringMap.new(), if true:
haxe_ds_StringMap.new().__set("+", Parser.choice)
haxe_ds_StringMap.new(), if true:
haxe_ds_StringMap.new().__set("#", Parser.tag)
haxe_ds_StringMap.new()]
static var symbols: Array[Variant] = []
static var choices: int = 0
var buffers: Array[HankBuffer] = []
@@ -32,6 +8,54 @@ var ast: Array[Variant] = []
func _init() -> void:
choices = 0
symbols.push_back({
"key": "INCLUDE ",
"value": Parser.include
})
symbols.push_back({
"key": "<-",
"value": Parser.thread
})
symbols.push_back({
"key": "->",
"value": Parser.divert
})
symbols.push_back({
"key": "===",
"value": Parser.knot
})
symbols.push_back({
"key": "==",
"value": Parser.knot
})
symbols.push_back({
"key": "=",
"value": Parser.stitch
})
symbols.push_back({
"key": "~",
"value": Parser.haxeLine
})
symbols.push_back({
"key": "```",
"value": Parser.haxeBlock
})
symbols.push_back({
"key": "-",
"value": Parser.gather
})
symbols.push_back({
"key": "*",
"value": Parser.choice
})
symbols.push_back({
"key": "+",
"value": Parser.choice
})
symbols.push_back({
"key": "#",
"value": Parser.tag
})
func parseString(h: String) -> Array[Variant]:
var stringBuffer: HankBuffer = HankBuffer.Dummy(h)
@@ -137,8 +161,8 @@ static func parseExpr(buffer: HankBuffer, position: Position) -> Variant:
while (_g2 < _g1.size()):
var rule: Variant = _g1[_g2]
_g2 += 1
var symbol: String = rule.keys().get("next").call()
var rule2 = rule.__get(symbol)
var symbol: String = rule.get("key")
var rule2 = rule.get("value")
if (StringTools.startsWith(StringTools.trim(line2), symbol)):
buffer.skipWhitespace("")
return rule2.call(buffer, position)
@@ -146,6 +170,8 @@ static func parseExpr(buffer: HankBuffer, position: Position) -> Variant:
1:
assert(false, str("Tried to parse expr when no lines were left in file"))
return null
static func lineTokens(buffer: HankBuffer, n: int, position: Position, throwOnMismatch: bool = true, rtrim: bool = true) -> Array[String]:
var line: String = Extensions.unwrap(buffer.takeLine(""))

View File

@@ -53,7 +53,7 @@
"CallStack.gd",
"Alt.gd"
],
"id": 25,
"id": 29,
"wasCached": false,
"version": 1
}

View File

@@ -7,32 +7,31 @@ import hank.HankAST.ExprType;
import hank.Choice.Choice;
import hank.HankBuffer;
typedef RuleFunction = HankBuffer->HankBuffer.Position->ExprType;
/**
Parses Hank scripts into ASTs for a Story object to interpret. Additional parsing happens in Alt.hx and Output.hx
**/
@:allow(tests.ParserTest)
class Parser {
// @formatter: off
static var symbols:Array<Map<String, HankBuffer->HankBuffer.Position->ExprType>> = [
['INCLUDE ' => include],
['<-' => thread],
['->' => divert],
['===' => knot],
['==' => knot],
['=' => stitch],
['~' => haxeLine],
['```' => haxeBlock],
['-' => gather],
['*' => choice],
['+' => choice],
['#' => tag]];
// @formatter: on
static var symbols:Array<{key:String, value:RuleFunction}> = [];
var buffers:Array<HankBuffer> = [];
var ast:HankAST = [];
public function new() {
choices = 0;
symbols.push({key: 'INCLUDE ', value: include});
symbols.push({key: '<-', value: thread});
symbols.push({key: '->', value: divert});
symbols.push({key: '===', value: knot});
symbols.push({key: '==', value: knot});
symbols.push({key: '=', value: stitch});
symbols.push({key: '~', value: haxeLine});
symbols.push({key: '```', value: haxeBlock});
symbols.push({key: '-', value: gather});
symbols.push({key: '*', value: choice});
symbols.push({key: '+', value: choice});
symbols.push({key: '#', value: tag});
}
public function parseString(h:String):HankAST {
@@ -113,8 +112,8 @@ class Parser {
}
for (rule in symbols) {
var symbol = rule.keys().next();
var rule = rule[symbol];
var symbol = rule.key;
var rule = rule.value;
if (line.trim().startsWith(symbol)) {
buffer.skipWhitespace();
return rule(buffer, position);
@@ -123,6 +122,7 @@ class Parser {
return output(buffer, position);
}
untyped __gdscript__('return null');
}
/** Split the given line into n tokens, throwing an error if there are any number of tokens other than n **/