ProductPromotion
Logo

Lua

made by https://0x3d.site

GitHub - andremm/lua-parser: A Lua 5.3 parser written with LPegLabel
A Lua 5.3 parser written with LPegLabel. Contribute to andremm/lua-parser development by creating an account on GitHub.
Visit Site

GitHub - andremm/lua-parser: A Lua 5.3 parser written with LPegLabel

GitHub - andremm/lua-parser: A Lua 5.3 parser written with LPegLabel

lua-parser

Build Status

This is a Lua 5.3 parser written with LPegLabel that generates an AST in a format that is similar to the one specified by Metalua. The parser uses LPegLabel to provide more specific error messages.

Requirements

    lua >= 5.1
    lpeglabel >= 1.6.0

API

The package lua-parser has two modules: lua-parser.parser and lua-parser.pp.

The module lua-parser.parser implements the function parser.parse:

  • parser.parse (subject, filename)

    Both subject and filename should be strings. It tries to parse subject and returns the AST in case of success. It returns nil plus an error message in case of error. In case of error, the parser uses the string filename to build an error message.

The module lua-parser.pp implements a pretty printer to the AST and a dump function:

  • pp.tostring (ast)

    It converts the AST to a string and returns this string.

  • pp.print (ast)

    It converts the AST to a string and prints this string.

  • pp.dump (ast[, i])

    It dumps the AST to the screen. The parameter i sets the indentation level.

AST format

block: { stat* }

stat:
        `Do{ stat* }
      | `Set{ {lhs+} {expr+} }                    -- lhs1, lhs2... = e1, e2...
      | `While{ expr block }                      -- while e do b end
      | `Repeat{ block expr }                     -- repeat b until e
      | `If{ (expr block)+ block? }               -- if e1 then b1 [elseif e2 then b2] ... [else bn] end
      | `Fornum{ ident expr expr expr? block }    -- for ident = e, e[, e] do b end
      | `Forin{ {ident+} {expr+} block }          -- for i1, i2... in e1, e2... do b end
      | `Local{ {ident+} {expr+}? }               -- local i1, i2... = e1, e2...
      | `Localrec{ ident expr }                   -- only used for 'local function'
      | `Goto{ <string> }                         -- goto str
      | `Label{ <string> }                        -- ::str::
      | `Return{ <expr*> }                        -- return e1, e2...
      | `Break                                    -- break
      | apply

expr:
        `Nil
      | `Dots
      | `Boolean{ <boolean> }
      | `Number{ <number> }
      | `String{ <string> }
      | `Function{ { `Id{ <string> }* `Dots? } block }
      | `Table{ ( `Pair{ expr expr } | expr )* }
      | `Op{ opid expr expr? }
      | `Paren{ expr }       -- significant to cut multiple values returns
      | apply
      | lhs

apply:
         `Call{ expr expr* }
       | `Invoke{ expr `String{ <string> } expr* }

lhs: `Id{ <string> } | `Index{ expr expr }

opid:  -- includes additional operators from Lua 5.3 and all relational operators
        'add'  | 'sub' | 'mul'  | 'div'
      | 'idiv' | 'mod' | 'pow'  | 'concat'
      | 'band' | 'bor' | 'bxor' | 'shl' | 'shr'
      | 'eq'   | 'ne'  | 'lt'   | 'gt'  | 'le'   | 'ge'
      | 'and'  | 'or'  | 'unm'  | 'len' | 'bnot' | 'not'

Usage

Code example for parsing a string:

local parser = require "lua-parser.parser"
local pp = require "lua-parser.pp"

if #arg ~= 1 then
    print("Usage: parse.lua <string>")
    os.exit(1)
end

local ast, error_msg = parser.parse(arg[1], "example.lua")
if not ast then
    print(error_msg)
    os.exit(1)
end

pp.print(ast)
os.exit(0)

Running the above code example using a string without syntax error:

$ lua parse.lua "for i=1, 10 do print(i) end"
{ `Fornum{ `Id "i", `Number "1", `Number "10", { `Call{ `Id "print", `Id "i" } } } }

Running the above code example using a string with syntax error:

$ lua parse.lua "for i=1, 10 do print(i) "
example.lua:1:24: syntax error, expected 'end' to close the for loop

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory