首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用lpeg解析类似TeX的语言

用lpeg解析类似TeX的语言
EN

Stack Overflow用户
提问于 2014-02-07 08:01:01
回答 1查看 498关注 0票数 3

我正努力让我的头围绕着LPEG。我已经想出了一种能做我想做的事的语法,但我一直在和这个语法搏斗,但没走多远。其思想是解析一个文档,它是TeX的一种简化形式。我想把一份文件分成:

  • 环境,即\begin{cmd}\end{cmd}对。
  • 命令,这些命令可以接受像:\foo{bar}这样的参数,也可以是裸的:\foo
  • 环境和命令都可以有如下所示的参数:\command[color=green,background=blue]{content}
  • 其他的东西。

为了处理错误,我还想跟踪行号信息。到目前为止,我的情况如下:

代码语言:javascript
复制
lpeg = require("lpeg")
lpeg.locale(lpeg)
-- Assume a lot of "X = lpeg.X" here.

-- Line number handling from http://lua-users.org/lists/lua-l/2011-05/msg00607.html
-- with additional print statements to check they are working.
local newline = P"\r"^-1 * "\n" / function (a) print("New"); end
local incrementline = Cg( Cb"linenum" )/ function ( a ) print("NL");  return a + 1 end , "linenum"
local setup = Cg ( Cc ( 1) , "linenum" )
nl = newline * incrementline
space = nl + lpeg.space

-- Taken from "Name-value lists" in http://www.inf.puc-rio.br/~roberto/lpeg/
local identifier = (R("AZ") + R("az") + P("_") + R("09"))^1
local sep = lpeg.S(",;") * space^0
local value = (1-lpeg.S(",;]"))^1
local pair = lpeg.Cg(C(identifier) * space ^0 * "=" * space ^0 * C(value)) * sep^-1
local list = lpeg.Cf(lpeg.Ct("") * pair^0, rawset)
local parameters = (P("[") * list * P("]")) ^-1

-- And the rest is mine

anything = C( (space^1 + (1-lpeg.S("\\{}")) )^1) * Cb("linenum") / function (a,b) return { text = a, line = b } end

begin_environment = P("\\begin") * Ct(parameters) * P("{") * Cg(identifier, "environment") * Cb("environment") * P("}") / function (a,b) return { params = a[1], environment = b } end
end_environment = P("\\end{") * Cg(identifier) * P("}") 

texlike = lpeg.P{
  "document";
  document = setup * V("stuff") * -1,
  stuff = Cg(V"environment" + anything + V"bracketed_stuff" + V"command_with" + V"command_without")^0,
  bracketed_stuff = P"{" * V"stuff" * P"}" / function (a) return a end,
  command_with =((P("\\") * Cg(identifier) * Ct(parameters) * Ct(V"bracketed_stuff"))-P("\\end{")) / function (i,p,n) return { command = i, parameters = p, nodes = n } end,
  command_without = (( P("\\") * Cg(identifier) * Ct(parameters) )-P("\\end{")) / function (i,p) return { command = i, parameters = p } end,
  environment = Cg(begin_environment * Ct(V("stuff")) * end_environment) / function (b,stuff, e) return { b = b, stuff = stuff, e = e} end
}

它几乎成功了!

代码语言:javascript
复制
> texlike:match("\\foo[one=two]thing\\bar")
{
  command = "foo",
  parameters = {
    {
      one = "two",
    },
  },
}
{
  line = 1,
  text = "thing",
}
{
  command = "bar",
  parameters = {
  },
}

但!首先,我根本无法让电话号码处理部分工作。incrementline中的函数从不被触发。

我也不知道嵌套捕获信息是如何传递给处理函数的(这就是为什么我在语法上半分散了CgCCt )。这意味着只从command_with中返回一个项。

代码语言:javascript
复制
> texlike:match("\\foo{text \\command moretext}")
{
  command = "foo",
  nodes = {
    {
      line = 1,
      text = "text ",
    },
  },
  parameters = {
  },
}

我也希望能够检查环境的开始和结束是否匹配,但是当我尝试这样做时,我从“开始”到“结束”时的反向引用不在范围内。我不知道从这里往哪里走。

EN

回答 1

Stack Overflow用户

发布于 2015-01-01 01:32:20

迟答,但希望它能提供一些洞察力,如果你还在寻找解决方案,或想知道问题是什么。

你的语法有几个问题,其中一些可能很难发现。

这里的行增量看起来不正确:

代码语言:javascript
复制
local incrementline = Cg( Cb"linenum" ) / 
                      function ( a ) print("NL");  return a + 1 end, 
                      "linenum"

看起来,您的目的是创建一个命名的捕获组,而不是匿名组。反捕获linenum实际上是作为一个变量使用的。问题是因为这是在匿名捕获中,linenum不会正确更新-- function(a)在调用时总是会收到1。您需要将关闭的)移到末尾,以便包含"linenum"

代码语言:javascript
复制
local incrementline = Cg( Cb"linenum" / 
                      function ( a ) print("NL");  return a + 1 end, 
                      "linenum")

相关的LPeg documentation用于Cg捕获。

第二个问题是anything非终端规则:

代码语言:javascript
复制
anything = C( (space^1 + (1-lpeg.S("\\{}")) )^1) * Cb("linenum") ...

这里有几件事要小心。首先,一个命名的Cg捕获(一旦incrementline规则被修复)不会产生任何结果,除非它在表中或者您备份它。第二件主要的事情是它有一个特殊的作用域,就像一个变量。更确切地说,当你在外部捕捉到它时,它的范围就结束了--就像你在这里做的那样:

代码语言:javascript
复制
C( (space^1 + (...) )^1)

这意味着当你用* Cb("linenum")引用它的反向捕获时,已经太晚了--你真正想要的linenum已经关闭了它的范围。

我总是觉得LPeg的re语法更容易理解,所以我用它重写了语法:

代码语言:javascript
复制
local grammar_cb =
{
  fold = pairfold, 
  resetlinenum = resetlinenum,
  incrementlinenum = incrementlinenum, getlinenum = getlinenum, 
  error = error
}

local texlike_grammar = re.compile(
[[
  document    <- '' -> resetlinenum {| docpiece* |} !.
  docpiece    <- {| envcmd |} / {| cmd |} / multiline
  beginslash  <- cmdslash 'begin'
  endslash    <- cmdslash 'end'
  envcmd      <- beginslash paramblock? {:beginenv: envblock :} (!endslash docpiece)*
                 endslash openbrace {:endenv: =beginenv :} closebrace / &beginslash {} -> error .
  envblock    <- openbrace key closebrace
  cmd         <- cmdslash {:command: identifier :} (paramblock? cmdblock)?
  cmdblock    <- openbrace {:nodes: {| docpiece* |} :} closebrace
  paramblock  <- opensq ( {:parameters: {| parampairs |} -> fold :} / whitesp) closesq
  parampairs  <- parampair (sep parampair)*
  parampair   <- key assign value
  key         <- whitesp { identifier }
  value       <- whitesp { [^],;%s]+ }
  multiline   <- (nl? text)+
  text        <- {| {:text: (!cmd !closebrace !%nl [_%w%p%s])+ :} {:line: '' -> getlinenum :} |}
  identifier  <- [_%w]+
  cmdslash    <- whitesp '\'
  assign      <- whitesp '='
  sep         <- whitesp ','
  openbrace   <- whitesp '{'
  closebrace  <- whitesp '}'
  opensq      <- whitesp '['
  closesq     <- whitesp ']'
  nl          <- {%nl+} -> incrementlinenum
  whitesp     <- (nl / %s)*
]], grammar_cb)

回调函数直接向前定义为:

代码语言:javascript
复制
local function pairfold(...)
  local t, kv = {}, ...
  if #kv % 2 == 1 then return ... end
  for i = #kv, 2, -2 do
    t[ kv[i - 1] ] = kv[i]
  end
  return t
end

local incrementlinenum, getlinenum, resetlinenum do
  local line = 1
  function incrementlinenum(nl)
    assert(not nl:match "%S")
    line = line + #nl
  end

  function getlinenum() return line end
  function resetlinenum() line = 1 end
end

使用具有多行的非平凡tex类str测试语法:

代码语言:javascript
复制
  local test1 = [[\foo{text \bar[color = red, background =   black]{
  moretext \baz{
even 
more text} }


this time skipping multiple

lines even, such wow!}]]

以lua-table格式生成以下AST:

代码语言:javascript
复制
{
  command = "foo",
  nodes = {
    {
      text = "text",
      line = 1
    },
    {
      parameters = {
        color = "red",
        background = "black"
      },
      command = "bar",
      nodes = {
        {
          text = "  moretext",
          line = 2
        },
        {
          command = "baz",
          nodes = {
            {
              text = "even ",
              line = 3
            },
            {
              text = "more text",
              line = 4
            }
          }
        }
      }
    },
    {
      text = "this time skipping multiple",
      line = 7
    },
    {
      text = "lines even, such wow!",
      line = 9
    }
  }
}

以及开始/结束环境的第二个测试:

代码语言:javascript
复制
  local test2 = [[\begin[p1
=apple,
p2=blue]{scope} scope foobar   
\end{scope} global foobar]]

似乎给出了你想要的东西:

代码语言:javascript
复制
{
  {
    {
      text = " scope foobar",
      line = 3
    },
    parameters = {
      p1 = "apple",
      p2 = "blue"
    },
    beginenv = "scope",
    endenv = "scope"
  },
  {
    text = " global foobar",
    line = 4
  }
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21622316

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档