You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If I call Positioned after ChainOperator, it will only work for the top-level node and may overwrite the position of the operand when there is no operation in the input.
For example (see the end for the full example code):
staticreadonlyParser<Node>parserExpression=
Parse.ChainOperator(parserOperator, parserInt,(op,lhs,rhs)=>new BinaryExpression(op, lhs, rhs)).Positioned();// only work for top-level
Is there a proper way to obtain the position information of the intermediate node?
Full example code
node.cs
using Sprache;using System.Collections.Generic;namespaceSphracheTest{publicabstractclassNode:IPositionAware<Node>{publicabstractIEnumerable<Node> Children {get;}publicPosition?StartPos{get;privateset;}publicintLength{get;privateset;}public Node SetPos(PositionstartPos,intlength){StartPos=startPos;Length=length;returnthis;}}}
literal.cs
using Sprache;using System;using System.Collections.Generic;namespaceSphracheTest{publicclassLiteral<T>:Node,IPositionAware<Literal<T>>{publicTValue{get;}publicLiteral(Tvalue)=>Value=value;publicoverrideIEnumerable<Node> Children => Array.Empty<Node>();publicoverridestring?ToString()=> Value?.ToString();Literal<T>IPositionAware<Literal<T>>.SetPos(PositionstartPos,intlength)=>(Literal<T>)SetPos(startPos, length);}}
binaryexpression.cs
using Sprache;using System;using System.Collections.Generic;namespaceSphracheTest{publicenumBinaryOperator{Plus,Subtract,}publicclassBinaryExpression:Node,IPositionAware<BinaryExpression>{publicoverrideIEnumerable<Node> Children =>new Node[]{ Left, Right };publicBinaryOperatorOperator{get;}publicNodeLeft{get;}publicNodeRight{get;}publicBinaryExpression(BinaryOperatorop,Nodelhs,Noderhs){Operator=op;Left=lhs;Right=rhs;}publicoverridestring?ToString()=>string.Join(GetOperatorString(), Left, Right);privatestringGetOperatorString()=>Operatorswitch{
BinaryOperator.Plus =>" + ",
BinaryOperator.Subtract =>" - ",
_ =>thrownew ArgumentException("Invalid operator", nameof(Operator)),};
BinaryExpression IPositionAware<BinaryExpression>.SetPos(PositionstartPos,intlength)=>(BinaryExpression)SetPos(startPos, length);}}
using Sprache;using System;namespaceSphracheTest{staticclassProgram{staticreadonlyParser<Node>parserInt=(fromnin Parse.Number selectnewLiteral<int>(int.Parse(n))).Positioned().Token();staticreadonlyParser<BinaryOperator>parserOperatorPlus=
Parse.Char('+').Return(BinaryOperator.Plus);staticreadonlyParser<BinaryOperator>parserOperatorSubtract=
Parse.Char('-').Return(BinaryOperator.Subtract);staticreadonlyParser<BinaryOperator>parserOperator=
parserOperatorPlus.XOr(parserOperatorSubtract).Token();staticreadonlyParser<Node>parserExpression=
Parse.ChainOperator(parserOperator, parserInt,(op,lhs,rhs)=>new BinaryExpression(op, lhs, rhs)).Positioned();// only work for top-levelstaticvoidMain(string[]args){
Console.WriteLine(parserExpression.Parse(Console.ReadLine()).ToTreeForm());}}}
The text was updated successfully, but these errors were encountered:
If I call
Positioned
afterChainOperator
, it will only work for the top-level node and may overwrite the position of the operand when there is no operation in the input.For example (see the end for the full example code):
input:
1 + 2 - 3 + 4
expected output:
actual output:
input:
1
(1 surrounded by space characters)expected output::
actual output:
Is there a proper way to obtain the position information of the intermediate node?
Full example code
node.cs
literal.cs
binaryexpression.cs
nodeextension.cs
program.cs
The text was updated successfully, but these errors were encountered: