GithubHelp home page GithubHelp logo

3c1u / antlr4rust Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rrevenantt/antlr4rust

0.0 0.0 0.0 765 KB

ANTLR4 parser generator runtime for Rust programming laguage

Rust 99.38% ANTLR 0.62%

antlr4rust's Introduction

antlr4rust

ANTLR4 runtime for Rust programming language

Tool(generator) part is currently located in rust-target branch of my antlr4 fork rrevenantt/antlr4/tree/rust-target Latest version is automatically built to releases on this repository. Also you can checkout it and mvn -DskipTests install

For examples you can see grammars, tests/gen for corresponding generated code and tests/my_tests.rs for actual usage examples

Implementation status

Everything is implemented, "business" logic is quite stable and well tested, but user facing API is not very robust yet an very likely will have some changes.

For now development is going on in this repository but eventually it will be merged to main ANTLR4 repo

Currently requires nightly version of rust. This likely will be the case until specialization,try_blocks and unsize features are stabilized.

Remaining things before merge:

  • API stabilization
    • Rust api guidelines compliance
    • more tests for API because it is quite different from Java
  • make parsing zero copy(i.e. use &str(or Cow) instead String in token and &Token in tree nodes)
  • more generic PredictionContext
  • generic over ownership for string
  • generate enum for labeled alternatives without redundant Error option
  • option to generate fields instead of getters by default
  • move useful exports to lib.rs for better documentation

Can be done after merge:

  • profiling and performance optimizations
  • Documentation
    • Some things are already documented but still far from perfect, also more links needed.
  • Code quality
    • Rustfmt fails to run currently
    • Clippy sanitation
    • Not all warning are fixed
  • cfg to not build potentially unnecessary parts (no Lexer if custom token stream, no ParserATNSimulator if LL(1) grammar)
  • visitor
  • run rustfmt on generated parser
Long term improvements
  • make tree generic over pointer type (requires GAT, otherwise it would be a problem for users that want ownership for parse tree)
  • support stable rust
  • support no_std(although alloc would still be required)

Usage

You use the ANTLR4 "tool" to generate a parser, that will use the ANTLR runtime, located here. You can run it with the following command:

java -jar <path to ANTLR4 tool> -Dlanguage=Rust MyGrammar.g4

For a full list of antlr4 tool options, please visit the tool documentation page.

You can also see build.rs as an example of build.rs configuration to rebuild parser automatically if grammar file was changed

Then add following to Cargo.toml of the crate from which generated parser is going to be used:

[dependencies]
lazy_static = "1.4"
antlr-rust = "0.1"

and #![feature(try_blocks)] in your project root module.

Parse Tree structure

It is possible to generate idiomatic Rust syntax trees. For this you would need to use labels feature of ANTLR tool. You can see Labels grammar for example. Consider following rule :

e   : a=e op='*' b=e   # mult
    | left=e '+' b=e   # add
		 

For such rule ANTLR will generate enum EContextAll containing mult and add alternatives, so you will be able to match on them in your code. Also corresponding struct for each alternative will contain fields you labeled. I.e. for MultContext struct will contain a and b fields containing child subtrees and op field with TerminalNode type which corresponds to individual Token. It also is possible to disable generic parse tree creation to keep only selected children via parser.build_parse_trees = false.

Differences with Java

Although Rust runtime API is made as close as possible to Java, there are quite some differences because Rust is not an OOP language and is much more explicit.

  • All rule context variables (rule argument or rule return) should implement Default + Clone.
  • If you are using labeled alternatives, struct generated for rule is a enum with variant for each alternative
  • Parser needs to have ownership for listeners, but it is possible te get listener back via ListenerId otherwise ParseTreeWalker should be used.
  • In embedded actions to access parser you should use recog variable instead of self. This is because predicate have to be inserted into two syntactically different places in generated parser

Unsafe

Currently unsafe is used only to cast from trait object back to original type and to update data inside Rc via get_mut_unchecked(returned mutable reference is used immediately and not stored anywhere)

Licence

BSD 3-clause

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.