mirror of
https://github.com/Smaug123/WoofWare.PrattParser
synced 2025-10-10 11:58:40 +00:00
Add array indexing
This commit is contained in:
@@ -20,24 +20,29 @@ module Example =
|
||||
| TokenType.Plus
|
||||
| TokenType.Minus
|
||||
| TokenType.Times
|
||||
| TokenType.Equal
|
||||
| TokenType.Factorial
|
||||
| TokenType.If
|
||||
| TokenType.Then
|
||||
| TokenType.Else
|
||||
| TokenType.ArrayIndex
|
||||
| TokenType.RightSquareBracket
|
||||
| TokenType.LeftBracket
|
||||
| TokenType.RightBracket -> None
|
||||
|
||||
let parser =
|
||||
Parser.make (fun token -> token.Type) atom
|
||||
|> Parser.withUnaryPostfix TokenType.Factorial (7, ()) Expr.factorial
|
||||
|> Parser.withUnaryPrefix TokenType.Plus ((), 5) id
|
||||
|> Parser.withUnaryPrefix TokenType.Minus ((), 5) Expr.unaryMinus
|
||||
|> Parser.withInfix TokenType.Plus (1, 2) Expr.plus
|
||||
|> Parser.withInfix TokenType.Minus (1, 2) Expr.minus
|
||||
|> Parser.withInfix TokenType.Times (1, 2) Expr.times
|
||||
|> Parser.withUnaryPostfix TokenType.Factorial (11, ()) Expr.factorial
|
||||
|> Parser.withUnaryPrefix TokenType.Plus ((), 9) id
|
||||
|> Parser.withUnaryPrefix TokenType.Minus ((), 9) Expr.unaryMinus
|
||||
|> Parser.withInfix TokenType.Plus (5, 6) Expr.plus
|
||||
|> Parser.withInfix TokenType.Minus (5, 6) Expr.minus
|
||||
|> Parser.withInfix TokenType.Times (7, 8) Expr.times
|
||||
|> Parser.withInfix TokenType.Equal (2, 1) Expr.equal
|
||||
|> Parser.withBracketLike
|
||||
TokenType.LeftBracket
|
||||
{
|
||||
ConsumeBeforeInitialToken = false
|
||||
ConsumeAfterFinalToken = false
|
||||
BoundaryTokens = [ TokenType.RightBracket ]
|
||||
Construct = Seq.exactlyOne >> Expr.paren
|
||||
@@ -45,6 +50,7 @@ module Example =
|
||||
|> Parser.withBracketLike
|
||||
TokenType.If
|
||||
{
|
||||
ConsumeBeforeInitialToken = false
|
||||
ConsumeAfterFinalToken = true
|
||||
BoundaryTokens = [ TokenType.Then ; TokenType.Else ]
|
||||
Construct =
|
||||
@@ -56,6 +62,7 @@ module Example =
|
||||
|> Parser.withBracketLike
|
||||
TokenType.If
|
||||
{
|
||||
ConsumeBeforeInitialToken = false
|
||||
ConsumeAfterFinalToken = true
|
||||
BoundaryTokens = [ TokenType.Then ]
|
||||
Construct =
|
||||
@@ -64,3 +71,15 @@ module Example =
|
||||
| [ ifClause ; thenClause ] -> Expr.ifThen ifClause thenClause
|
||||
| _ -> failwith "logic error"
|
||||
}
|
||||
|> Parser.withBracketLike
|
||||
TokenType.ArrayIndex
|
||||
{
|
||||
ConsumeBeforeInitialToken = true
|
||||
ConsumeAfterFinalToken = false
|
||||
BoundaryTokens = [ TokenType.RightSquareBracket ]
|
||||
Construct =
|
||||
fun s ->
|
||||
match s with
|
||||
| [ arg ; contents ] -> Expr.arrayIndex arg contents
|
||||
| _ -> failwith "logic error"
|
||||
}
|
||||
|
Reference in New Issue
Block a user