1

我正在尝试创建一个非常简单的 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 : {$1, $2, $3}.

actionlist -> elem : [$1].
actionlist -> elem 'pipe' actionlist : [$1 | $3].

elem -> function : $1.

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}}
4

1 回答 1

1

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

——托马斯·格伯特。

于 2016-02-24T02:45:56.093 回答