Giter Site home page Giter Site logo

antlr-swift-target's People

Contributors

frankl98 avatar janyou avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

antlr-swift-target's Issues

Generated visitor missing superclass methods

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:

feature - support for alternative outputs

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

I came to a problem

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

Codegen doesn't generate public constructor for SwiftBaseListener

Steps to reproduce:

  1. Generate some grammar.

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.

Question regarding Swift.stg

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?!

visit() always outputs nil

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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.