无法让 Yecc 和 Leex 工作

Having trouble getting Yecc and Leex to work

我正在尝试创建一个非常简单的 DSL,它采用格式类似于

的字符串
GET /endpoint controller.action1 |> controller.action2

然后把它变成类似

的东西
{"GET", "/endpoint", [{controller.action1}, {controller.action2}]}

我的 Leex 文件是这样的:

Definitions.

Rules.

GET|PUT|POST|DELETE|PATCH : {token, {method, TokenLine, TokenChars}}.
/[A-Za-z_]+ : {token, {endpoint, TokenLine, TokenChars}}.
[A-Za-z0-9_]+\.[A-Za-z0-9_]+ : {token, {function, TokenLine, splitControllerAction(TokenChars)}}.
\|\>  : {token, {pipe, TokenLine}}.
[\s\t\n\r]+ : skip_token.

Erlang code.
splitControllerAction(A) ->
   [Controller, Action] = string:tokens(A, "."),
   {list_to_atom(Controller), list_to_atom(Action)}.

我的 Yecc 文件如下所示:

Nonterminals route actionlist elem.
Terminals function endpoint method pipe.
Rootsymbol route.

route -> method endpoint actionlist : {, , }.

actionlist -> elem : [].
actionlist -> elem 'pipe' actionlist : [ | ].

elem -> function : .

Erlang code.

extract_token({_Token, _Line, Value}) -> _Token;

我得到的输出:

2> {ok, Fart, _} = blah:string("GET /asdfdsf dasfadsf.adsfasdf |> adsfsdf.adsfdf").
{ok,[{method,1,"GET"},
     {endpoint,1,"/asdfdsf"},
     {function,1,{dasfadsf,adsfasdf}},
     {pipe,1},
     {function,1,{adsfsdf,adsfdf}}],
    1}
3> blah_parser:parse(Fart).
{ok,{49,50,51}}

原来你需要用单引号将 括起来,否则它只会尝试成为 ASCII 值。

-Thomas Gebert。