Giter Club home page Giter Club logo

lua-parser.cs's Introduction

An out-of-box Lua parser

This is an antlr4 implementation of Lua parser. It has the following features:

  1. the grammar is compatible to LALR(1)/LR(1)
  2. the generated parser builds type-safe and declarative ASTs instead of error-prone CSTs -- that's why we call it "out-of-box".

Motivation

This project serves as a control group of comparisons against Typed BNF. We tend to show the conciseness, declarativity, simplicity, and other advantages of TypedBNF.

The grammar uses semantic actions to construct ASTs. The inner design pattern generally follows the philosophy described in "Abstract syntax from concrete syntax."(Wile, David S.), and the only exception is that we store leading tokens of concrete syntax it they are necessary to AST positioning.

AST Categories

block(suite: list<stmt>, ret: maybe<stmt>)
parameters(names: list<token>, vararg: maybe<token>)

arguments ::=
    | PositionalArgs(token, list<expr>)
    | TableArgs(table)
    | StringArg(token)

table(pos: token, fields: list<table_field>)
table_field ::=
    | IndexField(pos: token, value: expr, item: expr)
    | NameField(key: token, value: expr)
    | ElementField(expr)

range(low: expr, high: expr, step: maybe<expr>)

stmt ::=
    | ReturnStmt(token, list<expr>)
    | EmptyStmt(token)
    | Assignment(is_local bool, list<expr>, maybe<list<expr>>) -> stmt
    | ExprStmt(expr)
    | LabelStmt(token)
    | BreakStmt(token)
    | GotoStmt(pos: token, label: token)
    | DoStmt(token, block)
    | WhileStmt(token, expr, block)
    | RepeatStmt(token, block, expr)
    | IfStmt(token, expr, block, list<if_elseif>, maybe<if_else>)
    | ForRangeStmt(token, token, range, block)
    | ForInStmt: (token, list<token>, list<expr>, block)

expr ::=
    | Bin(op: token, l: expr, r: expr)
    | UnsolvedBin(list<op<expr>>)
    | Var(token)
    | Nil(token)
    | Bool(token, bool)
    | Num(token)
    | String(token)
    | Ellipse(token)
    | Exponent(expr, expr)
    | Len(token, expr)
    | Neg(token, expr)
    | Inv(token, expr)
    | Not(token, expr)
    | NestedExp(token, expr)
    | TableExpr(table)
    | CallFunc(expr, arguments)
    | CallMethod(expr, token, arguments)
    | Index(expr, expr)
    | Attr(expr, token)
    | FuncDef(pos: token, is_local: bool, fname: maybe<funcname>, params: maybe<params>, body: block)

funcname ::=
    | MethodName(funcname, token)
    | DotName(funcname, token)
    | VarName(token)

Run

shell> dotnet run

lua parser> 1 + 1
block { suite = System.Collections.Generic.List`1[lua_parser.stmt], ret = lua_parser.maybe`1[lua_parser.stmt] }lua parser> 

Stats of non-generated part

file characters lines description
Lua.g4 12480 349 define grammar
LuaAst.cs 3533 84 define ASTs
LuaParseRequire.cs 1428 60 define necessary operations
to construct ASTs

lua-parser.cs's People

Contributors

thautwarm avatar

Stargazers

RainsSoft avatar  avatar Raph avatar tomato avatar Anqur avatar Mutalisk avatar

Watchers

James Cloos avatar  avatar  avatar

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.