From 58461323b98e83b7fba66cb5de9be4bf289aca0a Mon Sep 17 00:00:00 2001 From: James Shubin Date: Fri, 6 Jun 2025 02:36:20 -0400 Subject: [PATCH] lang: parser: Try to add the end values in parser Not sure if this is right, but it's a start. --- .../polydoubleincludewithtype.txtar | 2 +- .../TestAstFunc2/fortytwoerror.txtar | 2 +- lang/interpret_test/TestAstFunc2/res1.txtar | 2 +- .../unify-interpolate-edge1-fail.txtar | 2 +- .../unify-interpolate-edge2-fail.txtar | 2 +- .../unify-interpolate-res-fail.txtar | 2 +- lang/parser/parser.y | 37 +++++++++++++------ 7 files changed, 31 insertions(+), 18 deletions(-) diff --git a/lang/interpret_test/TestAstFunc1/polydoubleincludewithtype.txtar b/lang/interpret_test/TestAstFunc1/polydoubleincludewithtype.txtar index 5a70008c..819fd7e1 100644 --- a/lang/interpret_test/TestAstFunc1/polydoubleincludewithtype.txtar +++ b/lang/interpret_test/TestAstFunc1/polydoubleincludewithtype.txtar @@ -12,4 +12,4 @@ class c1($a, $b []str) { } } -- OUTPUT -- -# err: errUnify: type error: list != str: /main.mcl @ 4:1-4:25 +# err: errUnify: type error: list != str: /main.mcl @ 4:1-4:26 diff --git a/lang/interpret_test/TestAstFunc2/fortytwoerror.txtar b/lang/interpret_test/TestAstFunc2/fortytwoerror.txtar index 29bcb97b..41936622 100644 --- a/lang/interpret_test/TestAstFunc2/fortytwoerror.txtar +++ b/lang/interpret_test/TestAstFunc2/fortytwoerror.txtar @@ -15,4 +15,4 @@ test ["x",] { float32 => $b, } -- OUTPUT -- -# err: errUnify: type error: int != float: /main.mcl @ 10:6-10:22 +# err: errUnify: type error: int != float: /main.mcl @ 10:6-10:25 diff --git a/lang/interpret_test/TestAstFunc2/res1.txtar b/lang/interpret_test/TestAstFunc2/res1.txtar index e88c6b52..6cbfa7e7 100644 --- a/lang/interpret_test/TestAstFunc2/res1.txtar +++ b/lang/interpret_test/TestAstFunc2/res1.txtar @@ -3,4 +3,4 @@ test "t1" { stringptr => 42, # int, not str } -- OUTPUT -- -# err: errUnify: type error: str != int: /main.mcl @ 2:2-2:17 +# err: errUnify: type error: str != int: /main.mcl @ 2:2-2:18 diff --git a/lang/interpret_test/TestAstFunc2/unify-interpolate-edge1-fail.txtar b/lang/interpret_test/TestAstFunc2/unify-interpolate-edge1-fail.txtar index 16151c64..416a0851 100644 --- a/lang/interpret_test/TestAstFunc2/unify-interpolate-edge1-fail.txtar +++ b/lang/interpret_test/TestAstFunc2/unify-interpolate-edge1-fail.txtar @@ -7,4 +7,4 @@ test "test" {} Test["${name}"] -> Test["test"] # must fail -- OUTPUT -- -# err: errUnify: type error: str != list: /main.mcl @ 6:1-6:15 +# err: errUnify: type error: str != list: /main.mcl @ 6:1-6:16 diff --git a/lang/interpret_test/TestAstFunc2/unify-interpolate-edge2-fail.txtar b/lang/interpret_test/TestAstFunc2/unify-interpolate-edge2-fail.txtar index 06e0c499..4c9d7d2c 100644 --- a/lang/interpret_test/TestAstFunc2/unify-interpolate-edge2-fail.txtar +++ b/lang/interpret_test/TestAstFunc2/unify-interpolate-edge2-fail.txtar @@ -7,4 +7,4 @@ test "test" {} Test["test"] -> Test["${name}"] # must fail -- OUTPUT -- -# err: errUnify: type error: str != list: /main.mcl @ 6:17-6:31 +# err: errUnify: type error: str != list: /main.mcl @ 6:17-6:32 diff --git a/lang/interpret_test/TestAstFunc2/unify-interpolate-res-fail.txtar b/lang/interpret_test/TestAstFunc2/unify-interpolate-res-fail.txtar index fc59296d..6229c4a4 100644 --- a/lang/interpret_test/TestAstFunc2/unify-interpolate-res-fail.txtar +++ b/lang/interpret_test/TestAstFunc2/unify-interpolate-res-fail.txtar @@ -5,4 +5,4 @@ $name = ["a", "bb", "ccc",] test "${name}" {} # must fail -- OUTPUT -- -# err: errUnify: type error: str != list: /main.mcl @ 4:1-4:17 +# err: errUnify: type error: str != list: /main.mcl @ 4:1-4:18 diff --git a/lang/parser/parser.y b/lang/parser/parser.y index e04eafc2..75540a7f 100644 --- a/lang/parser/parser.y +++ b/lang/parser/parser.y @@ -56,6 +56,9 @@ func init() { row int col int + endRow int + endCol int + //err error // TODO: if we ever match ERROR in the parser bool bool @@ -1568,11 +1571,10 @@ func pos(y yyLexer, dollar yySymType) { return } -// cast is used to pull out the parser run-specific struct we store our AST in. -// this is usually called in the parser. -func cast(y yyLexer) *lexParseAST { - x := y.(*Lexer).parseResult - return x.(*lexParseAST) +// posLast runs pos on the last token of the current stmt/expr. +func posLast(y yyLexer, dollars []yySymType) { + // pick the last token in the set matched by the parser + pos(y, dollars[len(dollars)-1]) // our pos } // locate should be called after creating AST nodes from lexer tokens to store @@ -1585,14 +1587,16 @@ func locate(y yyLexer, first yySymType, last yySymType, node interface{}) { if pn, ok := node.(interfaces.PositionableNode); !ok { return } else if !pn.IsSet() { - pn.Locate(first.row, first.col, last.row, last.col) + //fmt.Printf("LOCATE(%v): %v, %v, %v, %v\n", node, first.row, first.col, last.endRow, last.endCol) + pn.Locate(first.row, first.col, last.endRow, last.endCol) } } -// posLast runs pos on the last token of the current stmt/expr. -func posLast(y yyLexer, dollars []yySymType) { - // pick the last token in the set matched by the parser - pos(y, dollars[len(dollars)-1]) // our pos +// cast is used to pull out the parser run-specific struct we store our AST in. +// this is usually called in the parser. +func cast(y yyLexer) *lexParseAST { + x := y.(*Lexer).parseResult + return x.(*lexParseAST) } // cast is used to pull out the parser run-specific struct we store our AST in. @@ -1605,8 +1609,17 @@ func (yylex *Lexer) cast() *lexParseAST { func (yylex *Lexer) pos(lval *yySymType) { lval.row = yylex.Line() lval.col = yylex.Column() - // TODO: we could use: `s := yylex.Text()` to calculate a delta length! - //log.Printf("lexer: %d x %d", lval.row, lval.col) + + // use: `yylex.Text()` to calculate a delta length! + text := yylex.Text() + lines := strings.Split(text, "\n") + + lval.endRow = lval.row + len(lines) - 1 // add delta lines + if len(lines) > 1 { + lval.endCol = len(lines[len(lines)-1]) // add pos of last line + } else { + lval.endCol = lval.col + len(text) // moved over this many chars + } } // Error is the error handler which gets called on a parsing error.