janyou / antlr-swift-target Goto Github PK
View Code? Open in Web Editor NEWANTLR Swift Target
License: BSD 2-Clause "Simplified" License
ANTLR Swift Target
License: BSD 2-Clause "Simplified" License
I had to make a small fix in order to get tne the calculator example project (based on the Expr.g4 grammar) to work correctly.
I changed
public class ExprVisitor<T>: ParseTreeVisitor<T> {
to
public class ExprVisitor<T>: AbstractParseTreeVisitor<T> {
It's probably not good to make the change for long term and is inconsistent with the antlr4 java runtime, but works for now.
Here is the full code file for this class to paint a fuller picture:
I'm working on a python to swift converter using the code from objc2swift yahoo project.
https://github.com/yahoojapan/objc2swift
I can't get it to work without the scala files to match the tokens from the python3.g4 grammar.
https://github.com/antlr/grammars-v4/blob/master/python3/Python3.g4
I did open up a ticket - but no answers yet.
https://github.com/yahoojapan/objc2swift/issues/22
I found this java code for targeting scala. I think it's a bit out of date.
It would be awesome if this project supported other targets and I could just throw this in.
bdkent/antlr4-scala-target-example#1
Appreciate any help on this. Thanks
Potential list of targets
https://github.com/antlr/grammars-v4
Expr
: varName #VarExpr
| Expr (DIV | MUL) Expr #MultiplicativeExpr
| Expr (SUB | ADD) Expr #AdditiveExpr
| Expr LOGICAL Expr #LogicalExpr
| NOT Expr #NotExpr
| '(' Expr ')' #ParenthesizedExpr
| funcName '(' Expr (',' Expr)* ')' #FuncExpr
| number #NumExpr
;
syntax error: '#' came as a complete surprise to me while looking for lexer rule element
given gramma files for python / cpp / java / objc - spit out the respective gramma files.
Steps to reproduce:
Actual result:
public class SwiftBaseListener: SwiftListener {
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
public func enterTop_level(ctx: SwiftParser.Top_levelContext) { }
Expected result:
public class SwiftBaseListener: SwiftListener {
public init() {
}
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
public func enterTop_level(ctx: SwiftParser.Top_levelContext) { }
Notes:
Constructor should be public for use in frameworks.
hello, i noticed this line:
AttributeDecl(d) ::= "<d.name>: <SwiftTypeMap.(d.type)><if(d.initValue)> = <d.initValue><else>!<endif>"
in Swift.stg which adds forced unwrap to all attributes that don't have values in context objects. What's the rationale behind this? Cases are that if user wants to use ordinary optional it will add an extra "!" to the attribute, e.g.: public var name: Type?!
Hi!
I'm sure for 99% that it's me doing some wrong, but I don't know where I can ask questions related to Swift target...
I'm using this grammar from The Definitive ANTLR 4 Reference book to generate Swift target:
grammar LabeledExpr;
prog: stat+ ;
stat: expr NEWLINE # printExpr
| ID '=' expr NEWLINE # assign
| NEWLINE # blank
;
expr: expr op=('*'|'/') expr # MulDiv
| expr op=('+'|'-') expr # AddSub
| INT # int
| ID # id
| '(' expr ')' # parens
;
ID : [a-zA-Z]+ ;
INT : [0-9]+ ;
NEWLINE : '\r'? '\n' ;
WS : [ \t]+ -> skip ;
MUL : '*' ;
DIV : '/' ;
ADD : '+' ;
SUB : '-' ;
Then I subclass LabeledExprBaseVisitor with this code:
class EvalVisitor: LabeledExprBaseVisitor<Int> {
var memory: [String : Int?] = [:]
override func visitAssign(_ ctx: LabeledExprParser.AssignContext) -> Int? {
let id = ctx.ID()!.getText()
let value = visit(ctx.expr()!)
memory[id] = value
return value
}
override func visitPrintExpr(_ ctx: LabeledExprParser.PrintExprContext) -> Int? {
let value = visit(ctx.expr()!)
Swift.print(value)
return nil
}
override func visitId(_ ctx: LabeledExprParser.IdContext) -> Int? {
let id = ctx.ID()?.getText()
if let value = self.memory[id!] {
return value
} else {
return nil
}
}
override func visitMulDiv(_ ctx: LabeledExprParser.MulDivContext) -> Int? {
let left = visit(ctx.expr(0)!)
let right = visit(ctx.expr(1)!)
guard left != nil && right != nil else {
return nil
}
return ctx.op.getType() == LabeledExprParser.MUL ? left! * right! : left! / right!
}
override func visitAddSub(_ ctx: LabeledExprParser.AddSubContext) -> Int? {
let left = visit(ctx.expr(0)!)
let right = visit(ctx.expr(1)!)
guard left != nil && right != nil else {
return nil
}
return ctx.op.getType() == LabeledExprParser.ADD ? left! + right! : left! - right!
}
override func visitParens(_ ctx: LabeledExprParser.ParensContext) -> Int? {
return visit(ctx.expr()!)
}
}
Then I'm trying to produce an output:
let str = "193 \n" +
"a = 5 \n" +
"b = 6 \n" +
"a + b * 2 \n" +
"(1 + 2) * 3 \n"
let input = ANTLRInputStream(str)
let lexer = LabeledExprLexer(input)
let tokens = CommonTokenStream(lexer)
let parser = try! LabeledExprParser(tokens)
let tree = try! parser.prog()
let eval = EvalVisitor()
let _ = eval.visit(tree)
And I'm getting:
nil
nil
nil
I've found that in EvalVisitor the method self.visit(ctx.expr())
always outputs nil
. But ctx
's children are not nil
. What am I doing wrong? Or is it a bug?
Currently the code generation is done in java
Recently - getting some mileage using stencil - see this template as example
https://github.com/grpc/grpc-swift/tree/master/Plugin/Templates
I understand you consider this project complete and merged / abandoned - but as swift upgrades / I'm sure having swift do the generation would pay off down the track.
Also easier for native swift developers to come along and support.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.