Giter VIP home page Giter VIP logo

swift-ast's Introduction

Swift Abstract Syntax Tree

swift-ast master swift-lint master swift-transform pending

Travis CI Status codecov Swift 5.1 Swift Package Manager Platforms License

Swift Abstract Syntax Tree is an initiative to parse Swift Programming Language in Swift itself. The output of this utility is the corresponding Abstract Syntax Tree (AST) of the source code.

The AST produced in this tool is intended to be consumed in various scenarios. For example, source-to-source transformations like swift-transform and linting tools like swift-lint.

Refactoring, code manipulation and optimization can leverage this AST as well.

Other ideas could be llvm-codegen or jvm-codegen (thinking about JSwift) that consumes the AST and converts them into binary or bytecode. I have some proof-of-concepts, llswift-poc and jswift-poc, respectively. If you are interested in working on the codegens, send me email [email protected].

Swift Abstract Syntax Tree is part of Yanagiba Project. Yanagiba umbrella project is a toolchain of compiler modules, libraries, and utilities, written in Swift and for Swift.


A Work In Progress

The Swift Abstract Syntax Tree is still in active development. Though many features are implemented, some with limitations.

Pull requests for new features, issues and comments for existing implementations are welcomed.

Please also be advised that the Swift language is under rapid development, its syntax is not stable. So the details are subject to change in order to catch up as Swift evolves.

Requirements

Installation

Standalone Tool

To use it as a standalone tool, clone this repository to your local machine by

git clone https://github.com/yanagiba/swift-ast

Go to the repository folder, run the following command:

swift build -c release

This will generate a swift-ast executable inside .build/release folder.

Adding to swift Path (Optional)

It is possible to copy the swift-ast to the bin folder of your local Swift installation.

For example, if which swift outputs

/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Then you can copy swift-ast to it by

cp .build/release/swift-ast /Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-ast

Once you have done this, you can invoke swift-ast by calling swift ast in your terminal directly.

Embed Into Your Project

Add the swift-ast dependency to Package.swift:

// swift-tools-version:5.0

import PackageDescription

let package = Package(
  name: "MyPackage",
  dependencies: [
    .package(url: "https://github.com/yanagiba/swift-ast.git", from: "0.19.9")
  ],
  targets: [
    .target(name: "MyTarget", dependencies: ["SwiftAST+Tooling"]),
  ],
  swiftLanguageVersions: [.v5]
)

Usage

Command Line

Simply append the path of the file to swift-ast. It will dump the AST to the console.

swift-ast path/to/Awesome.swift

Multiple files can be parsed with one call:

swift-ast path1/to1/foo.swift path2/to2/bar.swift ... path3/to3/main.swift

CLI Options

By default, the AST output is in a plain text format without indentation nor color highlight to the keywords. The output format can be changed by providing the following option:

  • -print-ast: with indentation and color highlight
  • -dump-ast: in a tree structure
  • -diagnostics-only: no output other than the diagnostics information

In addition, -github-issue can be provided as the first argument option, and the program will try to generate a GitHub issue template with pre-filled content for you.

Use AST in Your Code

Loop Through AST Nodes

import AST
import Parser
import Source

do {
  let sourceFile = try SourceReader.read(at: filePath)
  let parser = Parser(source: sourceFile)
  let topLevelDecl = try parser.parse()

  for stmt in topLevelDecl.statements {
    // consume statement
  }
} catch {
  // handle errors
}

Traverse AST Nodes

We provide a pre-order depth-first traversal implementation on all AST nodes. In order to use this, simply write your visitor by conforming ASTVisitor protocol with the visit methods for the AST nodes that are interested to you. You can also write your own traversal implementations to override the default behaviors.

Returning false from traverse and visit methods will stop the traverse.

class MyVisitor : ASTVisitor {
  func visit(_ ifStmt: IfStatement) throws -> Bool {
    // visit this if statement

    return true
  }
}
let myVisitor = MyVisitor()
let topLevelDecl = MyParse.parse()
myVisitor.traverse(topLevelDecl)

Development

Build & Run

Building the entire project can be done by simply calling:

make

This is equivalent to

swift build

The dev version of the tool can be invoked by:

.build/debug/swift-ast path/to/file.swift

Running Tests

Compile and run the entire tests by:

make test

Contact

Ryuichi Sai

License

Swift Abstract Syntax Tree is available under the Apache License 2.0. See the LICENSE file for more info.

swift-ast's People

Contributors

angelolloqui avatar ryuichis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swift-ast's Issues

Trouble with variables whose value is a closure ?

Issue Summary

The code below is complete. Feel free to run it with swift foo.swift to see that it's valid and works. However, the parser is not able to deal with the closure. Specifically the arguments to the closure seem to be confusing the parser.

Environment

  • OS Info: macOS Version 10.14.1 (Build 18B75)
  • Yanagiba/swift-ast version: 0.18.10

Reproduction Steps

[Detailed steps to reproduce the issue.]

Sample code
let isPositive = { (_ value: Int) -> Bool in
    return value > 0
}
print(isPositive(100))

Command to run swift-ast with the code above:
swift-ast /Users/rudro/Downloads/foo.swift

Expected Result

What do you expect to happen as a result of the reproduction steps?

Actual Behavior

What currently happens as a result of the reproduction steps?

/Users/rudro/Downloads/foo.swift:1:23 fatal: expectedCloseParenTuple

Even Better

Is your project open sourced? If yes, can you point us to your repository?
If not, is it possible to make a small project that fails the Travis CI?
If not, can you create a gist with your sample code for us?

AST Node Starting Position

How can I get an AST Node starting position(number of characters elapsed already before this node) in the input swift file ?

Parse failure: guard let self = self else { return }

🚨New Issue Checklist🚨

  • [ x] Updated swift-ast to the latest version
  • [ x] I am aware that the swift-ast -github-issue /path/to/file.swift might help me generate this file

Issue Summary

The parser fails with expected expression for syntax like the following:

guard let self = self else { return }

Reproduction Steps

Sample code

networkRequest.fetchData() { [weak self] result in
	guard let self = self else { return }

	switch result {
	case .Succeeded(let data):
		self.processData(data)
	
	case .Failed(let err):
		self.handleError(err)
	}
}

It could be really nice if the parser could parse vaild Swift code - upgrade self from weak to strong.
https://github.com/apple/swift-evolution/blob/master/proposals/0079-upgrade-self-from-weak-to-strong.md

Expected Result

The code should be able to be parsed.

Actual Behavior

There is a fatal error: expected expression

Semicolons not handled as expected.

Issue Summary

Semicolons not handled as expected.

Environment

  • OS Info: macOS Version 10.13.1 (Build 17B48)
  • Yanagiba/swift-ast version: 0.4.1

Reproduction Steps

Every line which ends with a semicolon is treated as an error.

Sample code

class SemicolonIssue {
    
    private let foo = "";
    
}

Command to run swift-ast with the code above:
swift-ast SemicolonIssue.swift

Expected Result

A successfully parsed TopLevelDeclaration.

Actual Behavior

The parser failed with a badDeclaration error.

/Users/adfontes/Development/TDSoftware-GmbH/Apptracr/ios-monitoring-modul/ApptracrDemo/ApptracrDemo/swift-ast-issues/SemicolonIssue.swift:13:25 fatal: badDeclaration

Even Better

See the above sample class.

Semicolons not handled as expected

Issue Summary

Semicolons not handled as expected.

Environment

  • OS Info: macOS Version 10.13.1 (Build 17B48)
  • Yanagiba/swift-ast version: 0.4.1

Reproduction Steps

Every const_decl/var_decl which ends with a semicolon is treated as an error.

Sample code

struct SemicolonIssue {
    
    private let issue = "";
    
}

Command to run swift-ast with the code above:
swift-ast SemicolonIssue.swift

Expected Result

A successfully parsed TopLevelDeclaration.

Actual Behavior

The parser failed with a badDeclaration error.

SemicolonIssue.swift:13:25 fatal: badDeclaration

AST Node Starting Position

🚨New Issue Checklist🚨

  • Updated swift-ast to the latest version
  • I am aware that the swift-ast -github-issue /path/to/file.swift might help me generate this file

Issue Summary

[Insert a brief but thorough description of the issue]

Environment

  • OS Info: [Please insert the operating system name and version]
  • Yanagiba/swift-ast version: [Please insert the swift-ast version]

Reproduction Steps

[Detailed steps to reproduce the issue.]

Sample code
[Insert sample source code here]

Command to run swift-ast with the code above:
swift-ast sample.swift

Expected Result

What do you expect to happen as a result of the reproduction steps?

Actual Behavior

What currently happens as a result of the reproduction steps?

[
  Insert the current outputs and
  maybe other information that
  could help us understand the issue better
]

Even Better

Is your project open sourced? If yes, can you point us to your repository?
If not, is it possible to make a small project that fails the Travis CI?
If not, can you create a gist with your sample code for us?

Gets confused by extension on Integer.

Issue Summary

Gets confused by extension on Integer. What's interesting is the if you change 0.foo to 1.foo or some other number, it seems to work.

Environment

  • OS Info: macOS Version 10.14.1 (Build 18B75)
  • Yanagiba/swift-ast version: 0.18.10

Reproduction Steps

[Detailed steps to reproduce the issue.]

Sample code
extension Int {
    var foo: Int {
        return 10
    }
}

print(0.foo)

Command to run swift-ast with the code above:
swift-ast /Users/rudro/Downloads/foo.swift

Expected Result

What do you expect to happen as a result of the reproduction steps?

Actual Behavior

What currently happens as a result of the reproduction steps?

/Users/rudro/Downloads/foo.swift:7:10 fatal: expectedCloseParenFuncCall

Even Better

Is your project open sourced? If yes, can you point us to your repository?
If not, is it possible to make a small project that fails the Travis CI?
If not, can you create a gist with your sample code for us?

Swift 4.2 infinite loop

🚨New Issue Checklist🚨

  • Updated swift-ast to the latest version
  • I have read the Code of Conduct
  • I am aware that the swift-ast -github-issue /path/to/file.swift might help me generate this file

Issue Summary

When running Swift-AST tests with Xcode10, there is an infinite loop in the Token.Kind equatable extension. In concrete, it gets stuck in Sources -> Lexer -> TokenKind+Equatable.swift lines 19, 23 and 145

Environment

  • OS Info: MacOS 10.13.6
  • Yanagiba/swift-ast version: v0.4.3

Reproduction Steps

Build, install and run tests. The infinite loop happens just by running tests. (Better to do it inside Xcode GUI because it stops and lets to inspect. From console it just finishes with an Exited with signal code 11

Expected Result

Run tests normally

Actual Behavior

Exited with signal code 11 / infinite loop

Even Better

Detected from https://github.com/angelolloqui/SwiftKotlin when upgrading to Xcode10

swift-ast: command not found

Hi, I've followed the installation steps for "standalone tool", but when I tried the command line usage I got: "-bash: swift-ast: command not found".
I'm running swift-as on MacOSX High Sierra, with a swift version of 4.1.2

Parse failure on func(arg:)) syntax

Issue Summary

The parser fails with expected expression for syntax like the following:

return uti.flatMap(mimeType(uti:))

I included a sample of code from kickstarter/ios-oss, and have a few additional examples included below.

The sample code does seem to run successfully with swift 4.

Environment

  • OS Info: macOS Version 10.13.2 (Build 17C88)
  • Yanagiba/swift-ast version: 0.4.1

Reproduction Steps

Sample code
import Foundation

private func mimeType(extension: String, where: CFString? = nil) -> String? {
  let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
                                                  `extension` as CFString,
                                                  `where`)?.takeRetainedValue()
  return uti.flatMap(mimeType(uti:))
}

private func mimeType(uti: CFString) -> String? {
  return UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)?.takeRetainedValue() as String?
}

Command to run swift-ast with the code above:
swift-ast /Users/chrishulton/code/testing/swift_qa/failing_cases/expected_expression.swift

Expected Result

The code should be able to be parsed.

Actual Behavior

There is a fatal error: expected expression

/Users/chrishulton/code/testing/swift_qa/failing_cases/expected_expression.swift:7:35 fatal: expectedExpr

Even Better

Here are a few additional failing files for this case:

Let me know if I can provide any more information! Thanks!

Feature request: Parse swiftinterface files

🚨New Issue Checklist🚨

  • Updated swift-ast to the latest version
  • I am aware that the swift-ast -github-issue /path/to/file.swift might help me generate this file

Issue Summary

I am using swift-ast to parse Swift source code, and for that, it works great. However, I also need to be able to parse *.swiftinterface files, as that is what I can generate from Objective-C headers. Since I only care about the public interface, this is ideal. However, I can't parse these files with swift-ast because it exits with a DiagnosticStopper error.

I've had to try modifying the swiftinterface files to make them "look" like compilable Swift, for example, by adding empty brackets {} after function declarations. In this way I've been able to hackily use swift-ast for my purposes, but it's much too brittle to be effective.

I tried using SourceKitten directly to examine the structure, which was very promising as it works the same for swift and swiftinterface, but unfortunately it seems to be missing a bunch of stuff that I am currently able to get from swift-ast.

Reproduction Steps

Run swift-ast on any *.swiftinterface file

Expected Result

Code is parsed and I can navigate the tree, albeit with less detail.

Actual Behavior

DiagnosticStopped exit.

Can convert Swift AST to Swift source code?

🚨New Issue Checklist🚨

  • Updated swift-ast to the latest version
  • I am aware that the swift-ast -github-issue /path/to/file.swift might help me generate this file

Issue Summary

[Insert a brief but thorough description of the issue]

Environment

  • OS Info: [Please insert the operating system name and version]
  • Yanagiba/swift-ast version: [Please insert the swift-ast version]

Reproduction Steps

[Detailed steps to reproduce the issue.]

Sample code
[Insert sample source code here]

Command to run swift-ast with the code above:
swift-ast sample.swift

Expected Result

What do you expect to happen as a result of the reproduction steps?

Actual Behavior

What currently happens as a result of the reproduction steps?

[
  Insert the current outputs and
  maybe other information that
  could help us understand the issue better
]

Even Better

Is your project open sourced? If yes, can you point us to your repository?
If not, is it possible to make a small project that fails the Travis CI?
If not, can you create a gist with your sample code for us?

Parsing of pattern matching on type for catch clause.

🚨New Issue Checklist🚨

  • Updated swift-ast to the latest version
  • I have read the Code of Conduct
  • I am aware that the swift-ast -github-issue /path/to/file.swift might help me generate this file

Issue Summary

The parsing of type matching in the catch clause pattern fails, while swift itself can compile the file.

Environment

  • OS Info: Linux subsystem for Windows 10
  • Yanagiba/swift-ast version: 0.4.1

Reproduction Steps

Parse the included code example.

Sample code
enum someErr: Error {
        case woops
}

func badstuffhappens() throws -> String {
        throw someErr.woops
}

try? badstuffhappens()

try! badstuffhappens()

do {
        try badstuffhappens()
} catch someErr.woops {

}
catch is someErr {

}


Command to run swift-ast with the code above:
swift-ast /home/nathan/hw.swift

Actual Behavior

Parsing fails because the is keyword is not expected.

/home/nathan/hw.swift:26:7 fatal: expectedPattern

Swift comments are not output by the visitors

Hi, it appears that swift comments (and also whitespace) are just ignored by the various visitors, e.g. swift-ast -print-ast filename. This makes it unusable as a simple swift-to-other-language translator, which is a stated goal of the project.

How can I enable comments to be included in the output?
Thanks.

protocol method names are parsed same as protocolname

🚨New Issue Checklist🚨

  • [ no] Updated swift-ast to the latest version
  • [yes ] I am aware that the swift-ast -github-issue /path/to/file.swift might help me generate this file

Issue Summary

the ast output of a protocol method name comes out as same as protocol name

Environment

  • OS Info: Mac OS 10.14.4
  • Yanagiba/swift-ast version: 0.18.10

Reproduction Steps

Sample code
protocol TableUpdatable {
    func reloadTable()
    func errorLoadingData()
}

Command to run swift-ast with the code above:
swift-ast sample.swift

Expected Result

proto_decl
name: TableUpdatable
0: kind: method
name: reloadTable
1: kind: method
name: errorLoadingData

Actual Behavior

proto_decl
name: TableUpdatable
0: kind: method
name: TableUpdatable
1: kind: method
name: TableUpdatable

Get's confused by a switch statement which switches on CGPoints

Issue Summary

See the sample code, it's complete and can be run with a simple swift foo.swift to see the output. Note that the case statement confuses the parser.

Environment

  • OS Info: macOS Version 10.14.1 (Build 18B75)
  • Yanagiba/swift-ast version: 0.18.10

Reproduction Steps

[Detailed steps to reproduce the issue.]

Sample code
import Foundation

func foo(_ anchorPoint: CGPoint) {
    switch anchorPoint {
        // Top Left Corner
    case CGPoint(x: 1, y: 1):
        print("top left")
    default:
        print("other")
    }

}

foo(CGPoint(x: 1, y: 1))

Command to run swift-ast with the code above:
swift-ast /Users/rudro/Downloads/foo.swift

Expected Result

What do you expect to happen as a result of the reproduction steps?

Actual Behavior

What currently happens as a result of the reproduction steps?

/Users/rudro/Downloads/foo.swift:6:17 fatal: expectedCaseColon

Even Better

Is your project open sourced? If yes, can you point us to your repository?
If not, is it possible to make a small project that fails the Travis CI?
If not, can you create a gist with your sample code for us?

UInt64 literals cause runtime overflow errors.

🚨New Issue Checklist🚨

  • Updated swift-ast to the latest version
  • I am aware that the swift-ast -github-issue /path/to/file.swift might help me generate this file

Issue Summary

[Insert a brief but thorough description of the issue]

UInt64 literals such as 0xffffffffffffffff, cause runtime overflow errors at

integerPart = integerPart * rdx + value

Environment

  • OS Info: [Please insert the operating system name and version]
  • Yanagiba/swift-ast version: [Please insert the swift-ast version]

Reproduction Steps

[Detailed steps to reproduce the issue.]

Sample code
[Insert sample source code here]

Command to run swift-ast with the code above:
swift-ast sample.swift

Expected Result

What do you expect to happen as a result of the reproduction steps?

Actual Behavior

What currently happens as a result of the reproduction steps?

[
  Insert the current outputs and
  maybe other information that
  could help us understand the issue better
]

Even Better

Is your project open sourced? If yes, can you point us to your repository?
If not, is it possible to make a small project that fails the Travis CI?
If not, can you create a gist with your sample code for us?

Parse failure: expected '(' in parameter with generic operators

Issue Summary

I have seen files fail to parse with the error expected '(' in parameter. From what I can tell, these failures seem to be related to 1) using generics and 2) defining operators like == and +=, for example:

public func ==<A: Equatable, B: Equatable>(

I am not well versed in swift so apologies if this is not a detailed enough description. I have linked several OSS code samples below that cause this failure and seem related. The included snippet is from the airbnb/lona project and runs successfully in swift 4. Thanks!

Environment

  • OS Info: macOS Version 10.13.2 (Build 17C88)
  • Yanagiba/swift-ast version: 0.4.1

Reproduction Steps

Sample code
infix operator ?=

func ?=<T> (left: inout T?, right: T) {
    if left == nil {
        left = right
    }
}

Command to run swift-ast with the code above:
swift-ast /Users/chrishulton/code/testing/swift_qa/failing_cases/expected_paren.swift

Expected Result

The code should parse successfully.

Actual Behavior

There is a parse failure: expected '(' in parameter

/Users/chrishulton/code/testing/swift_qa/failing_cases/expected_paren.swift:3:9 fatal: expectedParameterOpenParenthesis

Even Better

Here are some additional OSS examples that cause this failure:

Feature request: Support attributes in TypeInheritance

🚨New Issue Checklist🚨

  • Updated swift-ast to the latest version
  • I am aware that the swift-ast -github-issue /path/to/file.swift might help me generate this file

Issue Summary

Environment

  • OS Info: [Please insert the operating system name and version]
  • Yanagiba/swift-ast version: [Please insert the swift-ast version]

Reproduction Steps

Try to parse:
class SomeClass: @unchecked Sendable

It will fail due to the @unchecked attribute. It doesn't seem the swift-ast type inheritance parser accepts attributes.

Command to run swift-ast with the code above:
swift-ast sample.swift

Expected Result

What do you expect to happen as a result of the reproduction steps?

Actual Behavior

What currently happens as a result of the reproduction steps?

[
  Insert the current outputs and
  maybe other information that
  could help us understand the issue better
]

Even Better

Is your project open sourced? If yes, can you point us to your repository?
If not, is it possible to make a small project that fails the Travis CI?
If not, can you create a gist with your sample code for us?

Exception while lexing a long decimal number

A long decimal number like -0.5773502691896257310588680411456152796745 causes an EXC_BAD_INSTRUCTION in the lexer at this line:

        let decimal = (fractionalPart?.decimal ?? 0) * rdx + value

How to get the data type of variable?

Issue Summary

Dear people, I want to get the data type of swift source code, but it's seems difficult if don't use swift intermediate code. If exists a tool can convieniently get data type or some solutions to get data type of swift source code

The issue has same issue.
#19 @yanagiba @ryuichis

Thank you

ExpectedPattern in field declaration of struct

Issue Summary

The variable defined as optional fails when parsed because of a failure to detect an expected pattern. However, as I don't see anything wrong with this code.

Side-note, the Swift 4 compiler parses this file (swiftc -parse <FILE>), I updated swift-ast to the latest version, and this file is not my own.

Environment

  • OS Info: Linux for Windows 10
  • Yanagiba/swift-ast version: 0.4.1
Sample code
/// Various types of fields
/// that can be used in a Schema.
public struct Field {
    public let name: String
    public let type: DataType
    public let optional: Bool
    public let unique: Bool
    public let `default`: Node?
    public let primaryKey: Bool

    public enum DataType {
        case id(type: IdentifierType)
        case int
        case string(length: Int?)
        case double
        case bool
        case bytes
        case date
        case custom(type: String)
    }

    public init(
        name: String,
        type: DataType,
        optional: Bool = false,
        unique: Bool = false,
        default: Node? = nil,
        primaryKey: Bool = false
    ) {
        self.name = name
        self.type = type
        self.optional = optional
        self.unique = unique
        self.default = `default`
        self.primaryKey = primaryKey
    }

    public init(
        name: String,
        type: DataType,
        optional: Bool = false,
        unique: Bool = false,
        default: NodeRepresentable? = nil,
        primaryKey: Bool = false
    ) {
        let node: Node?

        if let d = `default` {
            node = try? d.makeNode(in: rowContext)
        } else {
            node = nil
        }

        self.init(
            name: name,
            type: type,
            optional: optional,
            unique: unique,
            default: node,
            primaryKey: primaryKey
        )
    }
}

extension Field: Equatable {
    public static func ==(lhs: Field, rhs: Field) -> Bool {
        return lhs.name == rhs.name
            && lhs.type == rhs.type
            && lhs.optional == rhs.optional
            && lhs.unique == rhs.unique
            && lhs.default == rhs.default
            && lhs.primaryKey == rhs.primaryKey
    }
}

extension Field.DataType: Equatable {
    public static func ==(lhs: Field.DataType, rhs: Field.DataType) -> Bool {
        switch (lhs, rhs) {
        case (.id(let a), .id(let b)):
            return a == b
        case (.int, .int),
             (.string, .string),
             (.double, .double),
             (.bool, .bool),
             (.bytes, .bytes),
             (.date, .date):
            return true
        case (.custom(let a), .custom(let b)):
            return a == b
        default:
            return false
        }
    }
}

extension IdentifierType: Equatable {
    public static func ==(lhs: IdentifierType, rhs: IdentifierType) -> Bool {
        switch (lhs, rhs) {
        case (.int, .int),
             (.uuid, .uuid):
            return true
        case (.custom(let a), .custom(let b)):
            return a == b
        default:
            return false
        }
    }
}

Command to run swift-ast with the code above:
swift-ast /mnt/d/swift_repos/72896077/fluent/Sources/Fluent/Schema/Field.swift

Actual Behavior

Parser fails on line 7:16

/mnt/d/swift_repos/72896077/fluent/Sources/Fluent/Schema/Field.swift:7:16 fatal: expectedPattern

make xcodegen fails with error

Hi - I tried using make xcodegen to generate an Xcode project. It gives the following error:

error: could not find target(s): Source; use the 'path' property in the Swift 4 manifest to set a custom target path

Any idea what the problem is?

Thanks!

'postfix' as constant/variable name doesn't work

Issue Summary

'postfix' as constant/variable name doesn't work.

Environment

  • OS Info: macOS Version 10.13.1 (Build 17B48)
  • Yanagiba/swift-ast version: 0.4.1

Reproduction Steps

Every constant/variable named 'postfix' is treated as an error.

Sample code

struct PostfixIssue {
    
    private let postfix = ""
    
}

Command to run swift-ast with the code above:
swift-ast PostfixIssue.swift

Expected Result

A successfully parsed TopLevelDeclaration.

Actual Behavior

The parser failed with an expectedPattern error.

PostfixIssue.swift:13:17 fatal: expectedPattern

Type Resolutions with Standard Library

Do you know if we can currently do type resolutions for the AST using the Standard Library (perhaps by providing some sort of path to it) to allow for type resolutions beyond primitive type?

Parser crashes when using value matching in catch clause

🚨New Issue Checklist🚨

  • Updated swift-ast to the latest version
  • I have read the Code of Conduct
  • I am aware that the swift-ast -github-issue /path/to/file.swift might help me generate this file

Issue Summary

The file attached to this issue can be successfully built and executed using swiftc with no additional parameters.

Environment

  • OS Info: Linux Subsystem for Windows 10
  • Yanagiba/swift-ast version: 0.4.1

Reproduction Steps

Parse the included file using swift-ast -dump-ast <file> and observe that it fails to parse the value matching in the catch clause.

Sample code
enum VendingMachineError: Error {
        case insufficientFunds(coinsNeeded: Int)
}

func throwsSomething() throws -> String {

        return "This doesn't matter"

}


do {

        try throwsSomething()

}
catch VendingMachineError.insufficientFunds(3) {
        print("3 coins missing")
}
catch VendingMachineError.insufficientFunds(let coinsNeeded) {
        print("Not 3 coins missing")
}
catch {
        print("No coins missing")
}

Command to run swift-ast with the code above:
swift-ast /home/nathan/value_ex.swift

Expected Result

A valid ast dump for the test file, it appears that swift-ast does not correctly parse the type instantiation for the value matching.

Actual Behavior

/home/nathan/value_ex.swift:17:45 fatal: expectedPattern

not an issue - but fyi - ANTLR-Swift-Target / Antlr-Swift-Runtime

The ANTLR-Swift-Target was merged into antlr https://github.com/antlr/antlr4 official repo.

janyou/ANTLR-Swift-Target#10

https://github.com/johndpope/Antlr-Swift-Runtime

I built the java / golang / proto parser here
https://github.com/johndpope/Antlr-Swift-Runtime/tree/master/Test/Test

other languages here
https://github.com/johndpope/ANTLR-Swift-Target/tree/master/gen/grammars-v4

I'm interested to do machine learning language translation DRAGGNN with tensorflow by training on generated languages from one -> then translating the AST in SWIFT. eg. give me a golang class -> spit out the corresponding swift file.

let me know if your interested
made some progress here - https://github.com/nubbel/swift-tensorflow/

Gets confused by a typealias inside of a protocol

Issue Summary

The sample code included is complete. Run with swift foo.swift to verify this is valid code. Looks like the typealias line inside the protocol causes the error as it does not appear to be a "typical" protocol member.

Environment

  • OS Info: macOS Version 10.14.1 (Build 18B75)
  • Yanagiba/swift-ast version: 0.18.10

Reproduction Steps

[Detailed steps to reproduce the issue.]

Sample code
protocol MyProtocol {
    typealias Number = Int
    func update(value: Number)
}

print("hello")

Command to run swift-ast with the code above:
swift-ast /Users/rudro/Downloads/foo.swift

Expected Result

What do you expect to happen as a result of the reproduction steps?

Actual Behavior

What currently happens as a result of the reproduction steps?

/Users/rudro/Downloads/foo.swift:2:5 fatal: badProtocolMember

Even Better

Is your project open sourced? If yes, can you point us to your repository?
If not, is it possible to make a small project that fails the Travis CI?
If not, can you create a gist with your sample code for us?

Parsing subscript expressions with extensions fails

$ swift-ast test.swift
swift-ast/test.swift:11:15 fatal: expected ']' in expression list
let b = a[safe: i] ?? 0
              ^~~~


If you think this is a bug, please run
swift-ast -github-issue test.swift
and file a GitHub issue.

$ swift test.swift
b is 1
Sample code

This is test.swift

extension Collection {

    /// Returns the element at the specified index iff it is within bounds, otherwise nil.
    subscript (safe index: Index) -> Element? {
        return indices.contains(index) ? self[index] : nil
    }
}

let a = [0, 1, 2]
let i = 1
let b = a[safe: i] ?? 0
print("b is \(b)")

Shebang causes parse error

Shebang causes parse error

Shebangs are supported in swift files.

$ cat test.swift
#!/usr/bin/swift
print("hello world!")
$ chmod +x test.swift
$ ./test.swift
hello world!
$ swift test.swift
hello world!

However, swift-ast fails to parse a file with shebangs.

$ swift-ast test.swift
/Users/obonilla/o/swift-ast/test.swift:1:2 fatal: expected a valid keyword after '#' in compiler-control statement
#!/usr/bin/swift
 ^~~~

Feature Request: Support `async` keyword

🚨New Issue Checklist🚨

  • Updated swift-ast to the latest version
  • I am aware that the swift-ast -github-issue /path/to/file.swift might help me generate this file

Issue Summary

swift-ast cannot parse function declarations with Swift 5.5's async keyword.

Environment

  • OS Info:
  • Yanagiba/swift-ast version:

Reproduction Steps

Try to parse:

func foo() async

Expected Result

What do you expect to happen as a result of the reproduction steps?

Should parse successfully.

Actual Behavior

What currently happens as a result of the reproduction steps?

Diagnostic Stopper Error due to bad declaration

Case expressions not handled properly

Case expressions not handled properly

$ cat test.swift
let a = 10
let zero = 0
switch a {
  case zero..<5:
    print("less than 5\n")
  default:
    print("more than 5")
}
$ swift test.swift
more than 5
$ swift-ast test.swift
/Users/obonilla/o/swift-ast/test.swift:4:12 fatal: expected ':' after case items
  case zero..<5:
           ^~~~

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.