From a8b6ece30a54901084394f040f7d8a1d8f01d0b0 Mon Sep 17 00:00:00 2001 From: Sergio Medeiros Date: Mon, 23 Mar 2015 15:22:24 -0300 Subject: Small update of lpeg.html --- lpeglabel.html | 72 +++++++++++++++++++++++++++++++--------------------------- 1 file changed, 38 insertions(+), 34 deletions(-) (limited to 'lpeglabel.html') diff --git a/lpeglabel.html b/lpeglabel.html index b38e900..30e6636 100644 --- a/lpeglabel.html +++ b/lpeglabel.html @@ -10,14 +10,12 @@ - -
LPegLabel
@@ -86,14 +84,13 @@ provided by LpegLabel: if the matching of p1 gives one of l1, ..., ln %{l1, ..., ln} - Syntax of re module. Equivalent to lpeg.T(l1, ..., ln) + Syntax of relabel module. Equivalent to lpeg.T(l1, ..., ln) p1 /{l1, ..., ln} p2 - Syntax of re module. Equivalent to lpeg.Lc(p1, p2, l1, ..., ln) + Syntax of relabel module. Equivalent to lpeg.Lc(p1, p2, l1, ..., ln) -re.setlabels (tlabel) - Allows to specicify a table with labels. They keys of - tlabel must be integers, and the associated values usually are strings. +relabel.setlabels (tlabel) + Allows to specicify a table with mnemonic labels. @@ -136,13 +133,13 @@ labeled ordered choice pattern.

%{l1, ..., ln}

-Syntax of re module. Equivalent to lpeg.T(l1, ..., ln). +Syntax of relabel module. Equivalent to lpeg.T(l1, ..., ln).

p1 /{l1, ..., ln} p2

-Syntax of re module. Equivalent to lpeg.Lc(p1, p2, l1, ..., ln). +Syntax of relabel module. Equivalent to lpeg.Lc(p1, p2, l1, ..., ln).

@@ -152,11 +149,19 @@ The /{} operator is left-associative.

A grammar can use both choice operators (/ and /{}), but a single choice can not mix them. That is, the parser -of re module will not recognize a pattern as +of relabel module will not recognize a pattern as p1 / p2 /{l1} p3.

+

relabel.setlabels (tlabel)

+ +

Allows to specicify a table with labels. They keys of +tlabel must be integers between 0 and 31, +and the associated values should be strings. +

+ +

Some Examples

@@ -181,11 +186,11 @@ function mymatch (g, s) local r, e = g:match(s) if not r then if e == 1 then - return r, "Error: expecting an identifier" + return "Error: expecting an identifier" elseif e == 2 then - return r, "Error: expecting ','" + return "Error: expecting ','" else - return r, "Error" + return "Error" end end return r @@ -201,9 +206,8 @@ In this example we could think about writing rule List as follows: List = m.P(("," + m.T(2)) * m.V"Id")^0 but this would give us an expression that when matching -the end of input would result in a failure with a label -different from 0, that is equivalent to the regular failure, -and the result of the repetition would be this label. +the end of input would result in a failure whose associated +label would be 2.

@@ -235,7 +239,7 @@ local g = m.P{ function mymatch (g, s) local r, e = g:match(s) if not r then - return r, terror[e] + return terror[e] end return r end @@ -245,14 +249,14 @@ print(mymatch(g, "a b")) print(mymatch(g, ", b")) -

Throwing a label using the re module

+

Throwing a label using the relabel module

-We can also rewrite the previous example using the re module +We can also rewrite the previous example using the relabel module as follows:

-local re = require 're' 
+local re = require 'relabel' 
 
 local g = re.compile[[
   S    <- Id List
@@ -264,11 +268,11 @@ function mymatch (g, s)
   local r, e = g:match(s)
   if not r then
     if e == 1 then
-      return r, "Error: expecting an identifier"
+      return "Error: expecting an identifier"
     elseif e == 2 then
-      return r, "Error: expecting ','"
+      return "Error: expecting ','"
     else
-      return r, "Error"
+      return "Error"
     end
   end
   return r
@@ -280,12 +284,12 @@ print(mymatch(g, ", b"))
 

-Another way to describe the previous example using the re module -is by using a table with the description the errors (terror) and -another table that associates a name to a given label (tlabel): +Another way to describe the previous example using the relabel module +is by using a table with the description of the errors (terror) and +another table that associates a name to a given label (tlabels):

-local re = require 're' 
+local re = require 'relabel' 
 
 local errUndef, errId, errComma = 0, 1, 2
 
@@ -310,7 +314,7 @@ local g = re.compile[[
 function mymatch (g, s)
   local r, e = g:match(s)
   if not r then
-    return r, terror[e]
+    return terror[e]
   end
   return r
 end
@@ -369,7 +373,7 @@ a left-associative order as we did in the previous example that
 used Lc:
 

-local re = require're'
+local re = require'relabel'
 
 local terror = {} 
 
@@ -409,11 +413,11 @@ print(p:match(",b"))
 As a more complex example, below we have the grammar
 for the Tiny language, as described in 
 this paper.
-The example below can also how the line where the syntactic
+The example below can also show the line where the syntactic
 error probably happened.
 

-local re = require 're'
+local re = require 'relabel'
 
 local terror = {}
 
@@ -523,8 +527,8 @@ local g = re.compile([[
 
 

Download

-

LPeg -source code.

+

LPegLabel +source code.

License

-- cgit v1.2.3-55-g6feb