diff options
| author | Sergio Medeiros <sqmedeiros@gmail.com> | 2015-03-23 15:09:08 -0300 |
|---|---|---|
| committer | Sergio Medeiros <sqmedeiros@gmail.com> | 2015-03-23 15:09:08 -0300 |
| commit | f124b2d4449a13ac21af9581a44b4455d89eaddb (patch) | |
| tree | e0486555ac811c5f712927f110bb7bcda8569adf /examples | |
| parent | 0e93d536ba2d312502737cce2ab0cc21393c4842 (diff) | |
| download | lpeglabel-f124b2d4449a13ac21af9581a44b4455d89eaddb.tar.gz lpeglabel-f124b2d4449a13ac21af9581a44b4455d89eaddb.tar.bz2 lpeglabel-f124b2d4449a13ac21af9581a44b4455d89eaddb.zip | |
Renaming "re.lua" to "relabel.lua".
Adding examples, README and lpeglabel.html
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/listId1.lua | 27 | ||||
| -rw-r--r-- | examples/listId2.lua | 32 | ||||
| -rw-r--r-- | examples/listIdCatch.lua | 25 | ||||
| -rw-r--r-- | examples/listIdCatchRe.lua | 34 | ||||
| -rw-r--r-- | examples/listIdRe1.lua | 27 | ||||
| -rw-r--r-- | examples/listIdRe2.lua | 35 | ||||
| -rw-r--r-- | examples/tiny.lua | 163 |
7 files changed, 343 insertions, 0 deletions
diff --git a/examples/listId1.lua b/examples/listId1.lua new file mode 100644 index 0000000..e075bd1 --- /dev/null +++ b/examples/listId1.lua | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | local m = require'lpeglabel' | ||
| 2 | |||
| 3 | local g = m.P{ | ||
| 4 | "S", | ||
| 5 | S = m.V"Id" * m.V"List", | ||
| 6 | List = -m.P(1) + ("," + m.T(2)) * m.V"Id" * m.V"List", | ||
| 7 | Id = m.R'az'^1 + m.T(1), | ||
| 8 | } | ||
| 9 | |||
| 10 | function mymatch (g, s) | ||
| 11 | local r, e = g:match(s) | ||
| 12 | if not r then | ||
| 13 | if e == 1 then | ||
| 14 | return r, "Error: expecting an identifier" | ||
| 15 | elseif e == 2 then | ||
| 16 | return r, "Error: expecting ','" | ||
| 17 | else | ||
| 18 | return r, "Error" | ||
| 19 | end | ||
| 20 | end | ||
| 21 | return r | ||
| 22 | end | ||
| 23 | |||
| 24 | print(mymatch(g, "a,b")) | ||
| 25 | print(mymatch(g, "a b")) | ||
| 26 | print(mymatch(g, ", b")) | ||
| 27 | |||
diff --git a/examples/listId2.lua b/examples/listId2.lua new file mode 100644 index 0000000..4943368 --- /dev/null +++ b/examples/listId2.lua | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | local m = require'lpeglabel' | ||
| 2 | |||
| 3 | local errUndef = 0 | ||
| 4 | local errId = 1 | ||
| 5 | local errComma = 2 | ||
| 6 | |||
| 7 | local terror = { | ||
| 8 | [errUndef] = "Error", | ||
| 9 | [errId] = "Error: expecting an identifier", | ||
| 10 | [errComma] = "Error: expecting ','", | ||
| 11 | } | ||
| 12 | |||
| 13 | local g = m.P{ | ||
| 14 | "S", | ||
| 15 | S = m.V"Id" * m.V"List", | ||
| 16 | List = -m.P(1) + ("," + m.T(errComma)) * m.V"Id" * m.V"List", | ||
| 17 | Id = m.R'az'^1 + m.T(errId), | ||
| 18 | } | ||
| 19 | |||
| 20 | function mymatch (g, s) | ||
| 21 | local r, e = g:match(s) | ||
| 22 | if not r then | ||
| 23 | return r, terror[e] | ||
| 24 | end | ||
| 25 | return r | ||
| 26 | end | ||
| 27 | |||
| 28 | print(mymatch(g, "a,b")) | ||
| 29 | print(mymatch(g, "a b")) | ||
| 30 | print(mymatch(g, ", b")) | ||
| 31 | |||
| 32 | |||
diff --git a/examples/listIdCatch.lua b/examples/listIdCatch.lua new file mode 100644 index 0000000..38ad2e5 --- /dev/null +++ b/examples/listIdCatch.lua | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | local m = require'lpeglabel' | ||
| 2 | |||
| 3 | local errUndef, errId, errComma = 0, 1, 2 | ||
| 4 | |||
| 5 | local terror = { | ||
| 6 | [errUndef] = "Error", | ||
| 7 | [errId] = "Error: expecting an identifier", | ||
| 8 | [errComma] = "Error: expecting ','", | ||
| 9 | } | ||
| 10 | |||
| 11 | g = m.P{ | ||
| 12 | "S", | ||
| 13 | S = m.Lc(m.Lc(m.V"Id" * m.V"List", m.V"ErrId", errId), | ||
| 14 | m.V"ErrComma", errComma), | ||
| 15 | List = -m.P(1) + m.V"Comma" * m.V"Id" * m.V"List", | ||
| 16 | Id = m.R'az'^1 + m.T(errId), | ||
| 17 | Comma = "," + m.T(errComma), | ||
| 18 | ErrId = m.Cc(errId) / terror, | ||
| 19 | ErrComma = m.Cc(errComma) / terror | ||
| 20 | } | ||
| 21 | |||
| 22 | print(g:match("a,b")) | ||
| 23 | print(g:match("a b")) | ||
| 24 | print(g:match(",b")) | ||
| 25 | |||
diff --git a/examples/listIdCatchRe.lua b/examples/listIdCatchRe.lua new file mode 100644 index 0000000..5d38fec --- /dev/null +++ b/examples/listIdCatchRe.lua | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | local re = require're' | ||
| 2 | |||
| 3 | local terror = {} | ||
| 4 | |||
| 5 | local function newError(l, msg) | ||
| 6 | table.insert(terror, { l = l, msg = msg } ) | ||
| 7 | end | ||
| 8 | |||
| 9 | newError("errId", "Error: expecting an identifier") | ||
| 10 | newError("errComma", "Error: expecting ','") | ||
| 11 | |||
| 12 | local labelCode = {} | ||
| 13 | local labelMsg = {} | ||
| 14 | for k, v in ipairs(terror) do | ||
| 15 | labelCode[v.l] = k | ||
| 16 | labelMsg[v.l] = v.msg | ||
| 17 | end | ||
| 18 | |||
| 19 | re.setlabels(labelCode) | ||
| 20 | |||
| 21 | local p = re.compile([[ | ||
| 22 | S <- Id List /{errId} ErrId /{errComma} ErrComma | ||
| 23 | List <- !. / Comma Id List | ||
| 24 | Id <- [a-z]+ / %{errId} | ||
| 25 | Comma <- ',' / %{errComma} | ||
| 26 | ErrId <- '' -> errId | ||
| 27 | ErrComma <- '' -> errComma | ||
| 28 | ]], labelMsg) | ||
| 29 | |||
| 30 | print(p:match("a,b")) | ||
| 31 | print(p:match("a b")) | ||
| 32 | print(p:match(",b")) | ||
| 33 | |||
| 34 | |||
diff --git a/examples/listIdRe1.lua b/examples/listIdRe1.lua new file mode 100644 index 0000000..c75bb1d --- /dev/null +++ b/examples/listIdRe1.lua | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | local re = require 're' | ||
| 2 | |||
| 3 | local g = re.compile[[ | ||
| 4 | S <- Id List | ||
| 5 | List <- !. / (',' / %{2}) Id List | ||
| 6 | Id <- [a-z] / %{1} | ||
| 7 | ]] | ||
| 8 | |||
| 9 | function mymatch (g, s) | ||
| 10 | local r, e = g:match(s) | ||
| 11 | if not r then | ||
| 12 | if e == 1 then | ||
| 13 | return r, "Error: expecting an identifier" | ||
| 14 | elseif e == 2 then | ||
| 15 | return r, "Error: expecting ','" | ||
| 16 | else | ||
| 17 | return r, "Error" | ||
| 18 | end | ||
| 19 | end | ||
| 20 | return r | ||
| 21 | end | ||
| 22 | |||
| 23 | print(mymatch(g, "a,b")) | ||
| 24 | print(mymatch(g, "a b")) | ||
| 25 | print(mymatch(g, ", b")) | ||
| 26 | |||
| 27 | |||
diff --git a/examples/listIdRe2.lua b/examples/listIdRe2.lua new file mode 100644 index 0000000..67c42f1 --- /dev/null +++ b/examples/listIdRe2.lua | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | local re = require 're' | ||
| 2 | |||
| 3 | local errUndef, errId, errComma = 0, 1, 2 | ||
| 4 | |||
| 5 | local terror = { | ||
| 6 | [errUndef] = "Error", | ||
| 7 | [errId] = "Error: expecting an identifier", | ||
| 8 | [errComma] = "Error: expecting ','", | ||
| 9 | } | ||
| 10 | |||
| 11 | local tlabels = { ["errUndef"] = errUndef, | ||
| 12 | ["errId"] = errId, | ||
| 13 | ["errComma"] = errComma } | ||
| 14 | |||
| 15 | re.setlabels(tlabels) | ||
| 16 | |||
| 17 | local g = re.compile[[ | ||
| 18 | S <- Id List | ||
| 19 | List <- !. / (',' / %{errComma}) Id List | ||
| 20 | Id <- [a-z] / %{errId} | ||
| 21 | ]] | ||
| 22 | |||
| 23 | function mymatch (g, s) | ||
| 24 | local r, e = g:match(s) | ||
| 25 | if not r then | ||
| 26 | return r, terror[e] | ||
| 27 | end | ||
| 28 | return r | ||
| 29 | end | ||
| 30 | |||
| 31 | print(mymatch(g, "a,b")) | ||
| 32 | print(mymatch(g, "a b")) | ||
| 33 | print(mymatch(g, ", b")) | ||
| 34 | |||
| 35 | |||
diff --git a/examples/tiny.lua b/examples/tiny.lua new file mode 100644 index 0000000..aa9dbea --- /dev/null +++ b/examples/tiny.lua | |||
| @@ -0,0 +1,163 @@ | |||
| 1 | local re = require 're' | ||
| 2 | |||
| 3 | local terror = {} | ||
| 4 | |||
| 5 | local function newError(l, msg) | ||
| 6 | table.insert(terror, { l = l, msg = msg} ) | ||
| 7 | end | ||
| 8 | |||
| 9 | newError("errSemi", "Error: missing ';'") | ||
| 10 | newError("errExpIf", "Error: expected expression after 'if'") | ||
| 11 | newError("errThen", "Error: expected 'then' keyword") | ||
| 12 | newError("errCmdSeq1", "Error: expected at least a command after 'then'") | ||
| 13 | newError("errCmdSeq2", "Error: expected at least a command after 'else'") | ||
| 14 | newError("errEnd", "Error: expected 'end' keyword") | ||
| 15 | newError("errCmdSeqRep", "Error: expected at least a command after 'repeat'") | ||
| 16 | newError("errUntil", "Error: expected 'until' keyword") | ||
| 17 | newError("errExpRep", "Error: expected expression after 'until'") | ||
| 18 | newError("errAssignOp", "Error: expected ':=' in assigment") | ||
| 19 | newError("errExpAssign", "Error: expected expression after ':='") | ||
| 20 | newError("errReadName", "Error: expected an identifier after 'read'") | ||
| 21 | newError("errWriteExp", "Error: expected expression after 'write'") | ||
| 22 | newError("errSimpExp", "Error: expected '(', ID, or number after '<' or '='") | ||
| 23 | newError("errTerm", "Error: expected '(', ID, or number after '+' or '-'") | ||
| 24 | newError("errFactor", "Error: expected '(', ID, or number after '*' or '/'") | ||
| 25 | newError("errExpFac", "Error: expected expression after '('") | ||
| 26 | newError("errClosePar", "Error: expected ')' after expression") | ||
| 27 | |||
| 28 | local line | ||
| 29 | |||
| 30 | local function incLine() | ||
| 31 | line = line + 1 | ||
| 32 | return true | ||
| 33 | end | ||
| 34 | |||
| 35 | local function countLine(s, i) | ||
| 36 | line = 1 | ||
| 37 | local p = re.compile([[ | ||
| 38 | S <- (%nl -> incLine / .)* | ||
| 39 | ]], { incLine = incLine}) | ||
| 40 | p:match(s:sub(1, i)) | ||
| 41 | return true | ||
| 42 | end | ||
| 43 | |||
| 44 | local labelCode = {} | ||
| 45 | for k, v in ipairs(terror) do | ||
| 46 | labelCode[v.l] = k | ||
| 47 | end | ||
| 48 | |||
| 49 | re.setlabels(labelCode) | ||
| 50 | |||
| 51 | local g = re.compile([[ | ||
| 52 | Tiny <- CmdSeq | ||
| 53 | CmdSeq <- (Cmd (SEMICOLON / ErrSemi)) (Cmd (SEMICOLON / ErrSemi))* | ||
| 54 | Cmd <- IfCmd / RepeatCmd / ReadCmd / WriteCmd / AssignCmd | ||
| 55 | IfCmd <- IF (Exp / ErrExpIf) (THEN / ErrThen) (CmdSeq / ErrCmdSeq1) (ELSE (CmdSeq / ErrCmdSeq2) / '') (END / ErrEnd) | ||
| 56 | RepeatCmd <- REPEAT (CmdSeq / ErrCmdSeqRep) (UNTIL / ErrUntil) (Exp / ErrExpRep) | ||
| 57 | AssignCmd <- NAME (ASSIGNMENT / ErrAssignOp) (Exp / ErrExpAssign) | ||
| 58 | ReadCmd <- READ (NAME / ErrReadName) | ||
| 59 | WriteCmd <- WRITE (Exp / ErrWriteExp) | ||
| 60 | Exp <- SimpleExp ((LESS / EQUAL) (SimpleExp / ErrSimpExp) / '') | ||
| 61 | SimpleExp <- Term ((ADD / SUB) (Term / ErrTerm))* | ||
| 62 | Term <- Factor ((MUL / DIV) (Factor / ErrFactor))* | ||
| 63 | Factor <- OPENPAR (Exp / ErrExpFac) (CLOSEPAR / ErrClosePar) / NUMBER / NAME | ||
| 64 | ErrSemi <- ErrCount %{errSemi} | ||
| 65 | ErrExpIf <- ErrCount %{errExpIf} | ||
| 66 | ErrThen <- ErrCount %{errThen} | ||
| 67 | ErrCmdSeq1 <- ErrCount %{errCmdSeq1} | ||
| 68 | ErrCmdSeq2 <- ErrCount %{errCmdSeq2} | ||
| 69 | ErrEnd <- ErrCount %{errEnd} | ||
| 70 | ErrCmdSeqRep <- ErrCount %{errCmdSeqRep} | ||
| 71 | ErrUntil <- ErrCount %{errUntil} | ||
| 72 | ErrExpRep <- ErrCount %{errExpRep} | ||
| 73 | ErrAssignOp <- ErrCount %{errAssignOp} | ||
| 74 | ErrExpAssign <- ErrCount %{errExpAssign} | ||
| 75 | ErrReadName <- ErrCount %{errReadName} | ||
| 76 | ErrWriteExp <- ErrCount %{errWriteExp} | ||
| 77 | ErrSimpExp <- ErrCount %{errSimpExp} | ||
| 78 | ErrTerm <- ErrCount %{errTerm} | ||
| 79 | ErrFactor <- ErrCount %{errFactor} | ||
| 80 | ErrExpFac <- ErrCount %{errExpFac} | ||
| 81 | ErrClosePar <- ErrCount %{errClosePar} | ||
| 82 | ErrCount <- '' => countLine | ||
| 83 | ADD <- Sp '+' | ||
| 84 | ASSIGNMENT <- Sp ':=' | ||
| 85 | CLOSEPAR <- Sp ')' | ||
| 86 | DIV <- Sp '/' | ||
| 87 | IF <- Sp 'if' | ||
| 88 | ELSE <- Sp 'else' | ||
| 89 | END <- Sp 'end' | ||
| 90 | EQUAL <- Sp '=' | ||
| 91 | LESS <- Sp '<' | ||
| 92 | MUL <- Sp '*' | ||
| 93 | NAME <- Sp !RESERVED [a-z]+ | ||
| 94 | NUMBER <- Sp [0-9]+ | ||
| 95 | OPENPAR <- Sp '(' | ||
| 96 | READ <- Sp 'read' | ||
| 97 | REPEAT <- Sp 'repeat' | ||
| 98 | SEMICOLON <- Sp ';' | ||
| 99 | SUB <- Sp '-' | ||
| 100 | THEN <- Sp 'then' | ||
| 101 | UNTIL <- Sp 'until' | ||
| 102 | WRITE <- Sp 'write' | ||
| 103 | RESERVED <- (IF / ELSE / END / READ / REPEAT / THEN / UNTIL / WRITE) ![a-z]+ | ||
| 104 | Sp <- %s* | ||
| 105 | ]], { countLine = countLine }) | ||
| 106 | |||
| 107 | |||
| 108 | local function printError(n, e) | ||
| 109 | assert(n == nil) | ||
| 110 | print("Line " .. line .. ": " .. terror[e].msg) | ||
| 111 | end | ||
| 112 | |||
| 113 | local s = [[ | ||
| 114 | n := 5; | ||
| 115 | f := 1; | ||
| 116 | repeat | ||
| 117 | f := f + n; | ||
| 118 | n := n - 1 | ||
| 119 | until (n < 1); | ||
| 120 | write f;]] | ||
| 121 | printError(g:match(s)) | ||
| 122 | |||
| 123 | s = [[ | ||
| 124 | n := 5; | ||
| 125 | f := 1; | ||
| 126 | repeat | ||
| 127 | f := f + n; | ||
| 128 | n := n - 1; | ||
| 129 | until (n < 1); | ||
| 130 | read ;]] | ||
| 131 | printError(g:match(s)) | ||
| 132 | |||
| 133 | s = [[ | ||
| 134 | if a < 1 then | ||
| 135 | b := 2; | ||
| 136 | else | ||
| 137 | b := 3;]] | ||
| 138 | printError(g:match(s)) | ||
| 139 | |||
| 140 | s = [[ | ||
| 141 | n := 5; | ||
| 142 | f := 1; | ||
| 143 | repeat | ||
| 144 | f := f + n; | ||
| 145 | n := n - 1; | ||
| 146 | untill (n < 1); | ||
| 147 | ]] | ||
| 148 | printError(g:match(s)) | ||
| 149 | |||
| 150 | s = [[ | ||
| 151 | n := 5; | ||
| 152 | f := 1; | ||
| 153 | repeat | ||
| 154 | f := f + n; | ||
| 155 | n := n - 1; | ||
| 156 | 3 (n < 1); | ||
| 157 | ]] | ||
| 158 | printError(g:match(s)) | ||
| 159 | |||
| 160 | printError(g:match("a : 2")) | ||
| 161 | printError(g:match("a := (2")) | ||
| 162 | |||
| 163 | |||
