Skip to content

Commit

Permalink
Added the LPeg extension and an example project
Browse files Browse the repository at this point in the history
  • Loading branch information
astrochili committed Jun 23, 2020
1 parent f68706f commit 14fa6e2
Show file tree
Hide file tree
Showing 29 changed files with 7,958 additions and 2 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/.internal
/build
.externalToolBuilders
.DS_Store
Thumbs.db
.lock-wscript
*.pyc
.project
.cproject
builtins
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
# defold-lpeg
LPeg wrapper for the Defold game engine
# LPeg for Defold
LPeg extension for the Defold game engine. [LPeg](http://www.inf.puc-rio.br/~roberto/lpeg/) is a pattern-matching library by [Roberto Ierusalimschy](http://www.inf.puc-rio.br/~roberto/) for Lua.

The extension includes source code of LPeg 1.0.2 (MIT Licence).

## Installation

Add the link to the latest [release zip-archive](https://github.com/astrochili/defold-lpeg/releases) as a [library dependency](http://www.defold.com/manuals/libraries/) in the Defold project configuration.

## Usage

All operations are accessible through the global variable ```lpeg```.

```lua
local P = lpeg.P

local pattern = P'a' * P'b' ^ 0
local result = lpeg.match(pattern, 'abbc')

print(result) --> 4
```
37 changes: 37 additions & 0 deletions example/example.collection
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: "main"
scale_along_z: 0
embedded_instances {
id: "example"
data: "components {\n"
" id: \"main\"\n"
" component: \"/example/example.script\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}
Empty file added example/example.go
Empty file.
27 changes: 27 additions & 0 deletions example/example.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function init(self)
-- This example taken from the article by Leaf Corcoran, creator of MoonScript
-- https://leafo.net/guides/parsing-expression-grammars.html

local white = lpeg.S(" \t\r\n") ^ 0

local integer = white * lpeg.R("09") ^ 1 / tonumber
local muldiv = white * lpeg.C(lpeg.S("/*"))
local addsub = white * lpeg.C(lpeg.S("+-"))

local function node(p)
return p / function(left, op, right)
return { op, left, right }
end
end

local calculator = lpeg.P({
"input",
input = lpeg.V("exp") * -1,
exp = lpeg.V("term") + lpeg.V("factor") + integer,
term = node((lpeg.V("factor") + integer) * addsub * lpeg.V("exp")),
factor = node(integer * muldiv * (lpeg.V("factor") + integer))
})

local result = calculator:match("5*3*2")
pprint(result) --> {"*", 5, {"*", 3, 2}}
end
23 changes: 23 additions & 0 deletions game.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[bootstrap]
main_collection = /example/example.collectionc

[script]
shared_state = 1

[display]
width = 960
height = 640

[android]
input_method = HiddenInputField

[project]
title = LPeg for Defold

[library]
include_dirs = lpeg

[input]
game_binding = /builtins/input/all.input_bindingc
gamepads = /builtins/input/default.gamepadsc

1 change: 1 addition & 0 deletions lpeg/ext.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name: "lpeg"
27 changes: 27 additions & 0 deletions lpeg/src/lpeg.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
** LPeg extension for the Defold game engine
** https://github.com/astrochili/defold-lpeg
*/

#define LIB_NAME "lpeg"
#define MODULE_NAME "lpeg"

#include <dmsdk/sdk.h>
#include "wrapper.h"

dmExtension::Result InitializeMyExtension(dmExtension::Params* params)
{
luaopen_lpeg(params->m_L);
lua_pop(params->m_L, 2);

dmLogInfo("Registered %s Extension\n", MODULE_NAME);
return dmExtension::RESULT_OK;
}

dmExtension::Result FinalizeMyExtension(dmExtension::Params* params)
{
dmLogInfo("Finalize %s Extension\n", MODULE_NAME);
return dmExtension::RESULT_OK;
}

DM_DECLARE_EXTENSION(lpeg, LIB_NAME, 0, 0, InitializeMyExtension, 0, 0, FinalizeMyExtension)
100 changes: 100 additions & 0 deletions lpeg/src/lpeg/HISTORY
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
HISTORY for LPeg 1.0.2

* Changes from version 1.0.1 to 1.0.2
---------------------------------
+ some bugs fixed

* Changes from version 0.12 to 1.0.1
---------------------------------
+ group "names" can be any Lua value
+ some bugs fixed
+ other small improvements

* Changes from version 0.11 to 0.12
---------------------------------
+ no "unsigned short" limit for pattern sizes
+ mathtime captures considered nullable
+ some bugs fixed

* Changes from version 0.10 to 0.11
-------------------------------
+ complete reimplementation of the code generator
+ new syntax for table captures
+ new functions in module 're'
+ other small improvements

* Changes from version 0.9 to 0.10
-------------------------------
+ backtrack stack has configurable size
+ better error messages
+ Notation for non-terminals in 're' back to A instead o <A>
+ experimental look-behind pattern
+ support for external extensions
+ works with Lua 5.2
+ consumes less C stack

- "and" predicates do not keep captures

* Changes from version 0.8 to 0.9
-------------------------------
+ The accumulator capture was replaced by a fold capture;
programs that used the old 'lpeg.Ca' will need small changes.
+ Some support for character classes from old C locales.
+ A new named-group capture.

* Changes from version 0.7 to 0.8
-------------------------------
+ New "match-time" capture.
+ New "argument capture" that allows passing arguments into the pattern.
+ Better documentation for 're'.
+ Several small improvements for 're'.
+ The 're' module has an incompatibility with previous versions:
now, any use of a non-terminal must be enclosed in angle brackets
(like <B>).

* Changes from version 0.6 to 0.7
-------------------------------
+ Several improvements in module 're':
- better documentation;
- support for most captures (all but accumulator);
- limited repetitions p{n,m}.
+ Small improvements in efficiency.
+ Several small bugs corrected (special thanks to Hans Hagen
and Taco Hoekwater).

* Changes from version 0.5 to 0.6
-------------------------------
+ Support for non-numeric indices in grammars.
+ Some bug fixes (thanks to the luatex team).
+ Some new optimizations; (thanks to Mike Pall).
+ A new page layout (thanks to Andre Carregal).
+ Minimal documentation for module 're'.

* Changes from version 0.4 to 0.5
-------------------------------
+ Several optimizations.
+ lpeg.P now accepts booleans.
+ Some new examples.
+ A proper license.
+ Several small improvements.

* Changes from version 0.3 to 0.4
-------------------------------
+ Static check for loops in repetitions and grammars.
+ Removed label option in captures.
+ The implementation of captures uses less memory.

* Changes from version 0.2 to 0.3
-------------------------------
+ User-defined patterns in Lua.
+ Several new captures.

* Changes from version 0.1 to 0.2
-------------------------------
+ Several small corrections.
+ Handles embedded zeros like any other character.
+ Capture "name" can be any Lua value.
+ Unlimited number of captures.
+ Match gets an optional initial position.

(end of HISTORY)
8 changes: 8 additions & 0 deletions lpeg/src/lpeg/lauxlib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
** LPeg extension for the Defold game engine
** https://github.com/astrochili/defold-lpeg
**
** Placeholder for the lauxlib.h that required by LPeg.
*/

#include <dmsdk/sdk.h>
Loading

0 comments on commit 14fa6e2

Please sign in to comment.