量词在Lua句式中的应用

量词在Lua句式中的应用,lua,pattern-matching,lua-patterns,luajit,Lua,Pattern Matching,Lua Patterns,Luajit,因此,我试图使用Lua模式从C文件中解析出#define语句,但在多行定义中存在这样的情况,您可能会用反斜杠转义换行符 为了让我知道define结束的位置,我需要能够定义反斜杠+换行符,就像它是单个字符一样,这样我就可以得到它的补码,然后在它上面使用*量词,然后计数直到第一个非转义换行符 怎么做?您不能简单地用一些临时符号替换所有出现的“\\\n”,因为在下面的示例中,“c”行会出现问题。 相反,您应该为C源文件实现迷你扫描仪: local str = [[ #define x y #defin

因此,我试图使用Lua模式从C文件中解析出
#define
语句,但在多行定义中存在这样的情况,您可能会用反斜杠转义换行符

为了让我知道define结束的位置,我需要能够定义
反斜杠+换行符
,就像它是单个字符一样,这样我就可以得到它的补码,然后在它上面使用
*
量词,然后计数直到第一个非转义换行符


怎么做?

您不能简单地用一些临时符号替换所有出现的
“\\\n”
,因为在下面的示例中,
“c”
行会出现问题。
相反,您应该为C源文件实现迷你扫描仪:

local str = [[
#define x y
#define a b\
c\\
d();
#define z
]]

-- Print all #defines found in the text
local line = ""
for char in str:gmatch"\\?." do
   if char == "\n" then
      if line:sub(1, #"#define") == "#define" then
         print(line)
      end
      line = ""
   else
      line = line..char
   end
end
输出:

#define x y
#define a b\
c\\
#define z

您不能简单地用一些临时符号替换所有出现的
“\\\n”
,因为在下面的示例中,
“c”
行会出现问题。
相反,您应该为C源文件实现迷你扫描仪:

local str = [[
#define x y
#define a b\
c\\
d();
#define z
]]

-- Print all #defines found in the text
local line = ""
for char in str:gmatch"\\?." do
   if char == "\n" then
      if line:sub(1, #"#define") == "#define" then
         print(line)
      end
      line = ""
   else
      line = line..char
   end
end
输出:

#define x y
#define a b\
c\\
#define z

不能使用Lua模式量化序列。但是,您可以先用一些临时值替换序列,运行“收集您需要的匹配项”并进行后期处理来解决此问题。您无法使用Lua模式量化序列。但是,您可以先用一些临时值替换序列,然后运行“收集所需匹配项”和“后处理”来解决此问题。