Hi Dave..
I have been trying to use the Expression parser to build an AST for a project.... So I dove into the code of the library and pulled out your example in the ExpressionParser.swift file..
Here's my code:
import SwiftParsec
let java = LanguageDefinition<()>.javaStyle
let lexer = GenericTokenParser(languageDefinition: java)
let integer = lexer.integer
indirect enum T {
case I(Int)
case MulOp(T,T)
case DivOp(T,T)
case AddOp(T,T)
case SubOp(T,T)
}
func binary( name: String, function: (T, T) -> T, assoc: Associativity ) -> Operator<String, (), T> {
let opParser = StringParser.string(name) *> GenericParser(result: function)
return .infix(opParser, assoc)
}
let opTable: OperatorTable<String, (), T> = [
[
binary(name:"*", function: {a,b in return T.MulOp(a,b)}, assoc: .left),
binary(name:"/", function: {a,b in return T.DivOp(a,b)}, assoc: .left)
],
[
binary(name:"+", function: {a,b in return T.AddOp(a,b)}, assoc: .left),
binary(name:"-", function: {a,b in return T.SubOp(a,b)}, assoc: .left)
]
]
let openingParen = StringParser.character("(")
let closingParen = StringParser.character(")")
let decimal = GenericTokenParser<()>.decimal
let tint = {a in T.I(a)} <^> integer
let expression = opTable.makeExpressionParser { expression in
expression.between(openingParen, closingParen) <|>
tint <?> "simple expression"
} <?> "expression"
expression.runSafe(userState: (), sourceName:"", input: "345+143*54")
The problem is that i get an error "Playground execution failed: error: Untitled Page.xcplaygroundpage:56:24: error: cannot convert value of type 'GenericParser<String, (), (T, T) -> T>' to expected argument type 'GenericParser<_, , (, _) -> _>'
return .infix(opParser, assoc)
"
The error is referring to the type of opParser.
Everything appears ok to me... but then I'm not a swift (or functional programming) expert.
Any ideas?
Thanks
bob