Hey there! I am currently making a binary tree parser for elm with the following structure:
“[1,2,3]” becomes Node 2 1 3, indicating that the middle node gets swapped with the one on its left.
So far, this is what I’ve got:
module BinaryParser exposing (..)
import Debug exposing (..)
import Html exposing (Html)
import Parser exposing (..)
import Set exposing (..)
type Tree
= Nil
| Node Int Tree Tree
swapElement : Tree -> Int -> Tree -> Tree
swapElement left val right =
Node val left right
treeParser : Parser Tree
treeParser =
oneOf
[
succeed swapElement
|. symbol "["
|= lazy (\_ -> treeParser)
|. symbol ","
|= int
|. symbol ","
|= lazy (\_ -> treeParser)
|. symbol "]"
]
userInputParser : Parser Tree
userInputParser =
succeed identity
|= treeParser
|. end
binary = Parser.run userInputParser "[1,9,3]"
my_results: List String
my_results =
[
pr <| binary
]
The problem I have is quite confusing: it outputs the following:
Err [{ col = 2, problem = ExpectingSymbol “[”, row = 1 }]
This seems a bit odd to me as I have the ‘[’ symbol.
Any advice/ideas would be appreciated!