Giter Site home page Giter Site logo

lua-parser's Introduction

lua-parser

This is a Lua 5.3 parser written with LPeg that generates an AST in the format specified by Metalua. The parser also implements an error reporting technique that is based on tracking the farthest failure position.

Requirements

    lua >= 5.1
    lpeg >= 0.12

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.

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], "exemplo1.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) "
exemplo1.lua:1:24: syntax error, unexpected 'EOF', expecting 'end',
'return', 'Name', 'goto', 'break', '::', 'local', 'function', 'repeat',
'for', 'do', 'while', 'if', ';', '=', ',', 'String', '{', '(', ':', '[', '.'

lua-parser's People

Contributors

andremm avatar

Watchers

James Cloos avatar TsT 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.