Questions tagged [lpeg]

LPeg is a pattern-matching library for Lua, based on Parsing Expression Grammars (PEGs).

LPeg is a pattern-matching library for Lua, based on Parsing Expression Grammars (PEGs). It defines patterns as first-class objects that can be composed using regular Lua operators. Its powerful capture mechanisms allows to search, extract and transform strings, following regular expressions or full grammars.

Useful links:

43 questions
7
votes
1 answer

Need help to understand LPeg and PEGs

The following pattern (from this page) matches only strings with balanced parentheses: b = lpeg.P{ "(" * ((1 - lpeg.S"()") + lpeg.V(1))^0 * ")" } What does 1- in 1 - lpeg.S"()" mean? function gsub (s, patt, repl) patt = lpeg.P(patt) patt =…
Decula
  • 494
  • 4
  • 16
6
votes
1 answer

Using LPEG (Lua Parser Expression Grammars) like boost::spirit

So I am playing with lpeg to replace a boost spirit grammar, I must say boost::spirit is far more elegant and natural than lpeg. However it is a bitch to work with due to the constraints of current C++ compiler technology and the issues of TMP in…
Hassan Syed
  • 20,075
  • 11
  • 87
  • 171
6
votes
2 answers

Creating a recursive LPeg pattern

In a normal PEG (parsing expression grammar) this is a valid grammar: values <- number (comma values)* number <- [0-9]+ comma <- ',' However, if I try to write this using LPeg the recursive nature of that rule fails: local lpeg =…
Phrogz
  • 296,393
  • 112
  • 651
  • 745
5
votes
1 answer

LPeg grammar oddity

Part of a Lua application of mine is a search bar, and I'm trying to make it understand boolean expressions. I'm using LPeg, but the current grammar gives a strange result: > re, yajl = require're', require'yajl' > querypattern = re.compile[=[ …
mmirate
  • 704
  • 6
  • 25
4
votes
1 answer

Matching Unicode punctuation using LPeg

I am trying to create an LPeg pattern that would match any Unicode punctuation inside UTF-8 encoded input. I came up with the following marriage of Selene Unicode and LPeg: local unicode = require("unicode") local lpeg =…
Witiko
  • 3,167
  • 3
  • 25
  • 43
4
votes
5 answers

Parsing out multiple lines with LPeg in Lua

I have some text file with multiple lines block like 2011/01/01 13:13:13,, Some Certain Text,=, [ certain text [ 0: 0 0 0 0 0 0 0 0 8: 0 0 0 0 0 0 0 0 16: 0 0 0 9 343 3938 9433…
Decula
  • 494
  • 4
  • 16
4
votes
2 answers

lpeg parse first-order logic term

As the title says, I'm trying to parse for example term(A, b, c(d, "e", 7)) in a Lua table like {term, {A, b, {c, {d, "e", 7}}}} This is the grammar I built: local pattern = re.compile[=[ term <- variable / function argument <- variable…
キキジキ
  • 1,443
  • 1
  • 25
  • 44
4
votes
2 answers

lua pattern matching: delimited captures

I am trying to parse a string such as: &1 first &2 second &4 fourth \\, and from it to build a table t = {1=first, 2=second, 4=fourth} I'm not very experienced with regex in general so my naive try (disregarding the \\ and table parts for the…
Scott H.
  • 143
  • 4
3
votes
2 answers

Match repeatable string as a "whole word" in Lua 5.1

My environment: Lua 5.1 Absolutely no libraries with a native component (like a C .so/.dll) can be used I can run any arbitrary pure Lua 5.1 code, but I can't access os and several other packages that would allow access to the native filesystem,…
allquixotic
  • 1,481
  • 2
  • 18
  • 37
3
votes
1 answer

LPeg Pattern which matches strings without consecutive hypens

I'm trying to write an LPeg pattern to match strings which: begin with a letter thereafter contain alphanumeric characters does not contain two or more consecutive hyphens (e.g. disallows test--string) For reference, the regular expression…
junius
  • 570
  • 3
  • 14
3
votes
1 answer

How to match exactly n repetitions of a pattern in Lua?

I'm writing a grammar that includes exactly three alphabetic characters. Here's my code: local l = require "lpeg" l.locale(l) local date = l.digit^1 * l.P'/' * l.digit^1 * l.P'/' * l.digit^1 local time = l.digit^1 * l.P':' * l.digit^1 * l.P':' *…
hourback
  • 1,150
  • 4
  • 12
  • 27
3
votes
1 answer

How to do lookahead properly with LPeg

To match a string starting with dog, followed by cat(but not consuming cat), this works: local lpeg = require 'lpeg' local str1 = 'dogcat' local patt1 = lpeg.C(lpeg.P('dog')) * #lpeg.P('cat') print(lpeg.match(patt1, str1)) Output: dog To match a…
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
3
votes
1 answer

Parsing a TeX-like language with lpeg

I am struggling to get my head around LPEG. I have managed to produce one grammar which does what I want, but I have been beating my head against this one and not getting far. The idea is to parse a document which is a simplified form of TeX. I want…
Simon Cozens
  • 755
  • 4
  • 16
3
votes
2 answers

Installing moonscript via luarocks on Windows

When I try to install moonscript through luarocks, the process errors out in installing the dependencies for moonscript, saying that "cl" isn't an installed / recognized program. C:\Users\Kingdaro>luarocks install moonscript Installing…
kingdaro
  • 11,528
  • 3
  • 34
  • 38
3
votes
3 answers

How to make LPeg.match return nil

I'm currently getting familiar with the LPeg parser module. For this I want to match a version string (e.g. 11.4) against a list. Such a list is a string with a tight syntax that can also contain ranges. Here is an EBNF-like, but in any case quite…
dualed
  • 10,262
  • 1
  • 26
  • 29
1
2 3