yeccpre.hrl
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1996-2021. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The parser generator will insert appropriate declarations before this line.%
-type yecc_ret() :: {'error', _} | {'ok', _}.
-spec parse(Tokens :: list()) -> yecc_ret().
parse(Tokens) ->
yeccpars0(Tokens, {no_func, no_location}, 0, [], []).
-spec parse_and_scan({function() | {atom(), atom()}, [_]}
| {atom(), atom(), [_]}) -> yecc_ret().
parse_and_scan({F, A}) ->
yeccpars0([], {{F, A}, no_location}, 0, [], []);
parse_and_scan({M, F, A}) ->
Arity = length(A),
yeccpars0([], {{fun M:F/Arity, A}, no_location}, 0, [], []).
-spec format_error(any()) -> [char() | list()].
format_error(Message) ->
case io_lib:deep_char_list(Message) of
true ->
Message;
_ ->
io_lib:write(Message)
end.
%% To be used in grammar files to throw an error message to the parser
%% toplevel. Doesn't have to be exported!
-compile({nowarn_unused_function, return_error/2}).
-spec return_error(erl_anno:location(), any()) -> no_return().
return_error(Location, Message) ->
throw({error, {Location, ?MODULE, Message}}).
-define(CODE_VERSION, "1.4").
yeccpars0(Tokens, Tzr, State, States, Vstack) ->
try yeccpars1(Tokens, Tzr, State, States, Vstack)
catch
error: Error: Stacktrace ->
try yecc_error_type(Error, Stacktrace) of
Desc ->
erlang:raise(error, {yecc_bug, ?CODE_VERSION, Desc},
Stacktrace)
catch _:_ -> erlang:raise(error, Error, Stacktrace)
end;
%% Probably thrown from return_error/2:
throw: {error, {_Location, ?MODULE, _M}} = Error ->
Error
end.
yecc_error_type(function_clause, [{?MODULE,F,ArityOrArgs,_} | _]) ->
case atom_to_list(F) of
"yeccgoto_" ++ SymbolL ->
{ok,[{atom,_,Symbol}],_} = erl_scan:string(SymbolL),
State = case ArityOrArgs of
[S,_,_,_,_,_,_] -> S;
_ -> state_is_unknown
end,
{Symbol, State, missing_in_goto_table}
end.
yeccpars1([Token | Tokens], Tzr, State, States, Vstack) ->
yeccpars2(State, element(1, Token), States, Vstack, Token, Tokens, Tzr);
yeccpars1([], {{F, A},_Location}, State, States, Vstack) ->
case apply(F, A) of
{ok, Tokens, EndLocation} ->
yeccpars1(Tokens, {{F, A}, EndLocation}, State, States, Vstack);
{eof, EndLocation} ->
yeccpars1([], {no_func, EndLocation}, State, States, Vstack);
{error, Descriptor, _EndLocation} ->
{error, Descriptor}
end;
yeccpars1([], {no_func, no_location}, State, States, Vstack) ->
Line = 999999,
yeccpars2(State, '$end', States, Vstack, yecc_end(Line), [],
{no_func, Line});
yeccpars1([], {no_func, EndLocation}, State, States, Vstack) ->
yeccpars2(State, '$end', States, Vstack, yecc_end(EndLocation), [],
{no_func, EndLocation}).
%% yeccpars1/7 is called from generated code.
%%
%% When using the {includefile, Includefile} option, make sure that
%% yeccpars1/7 can be found by parsing the file without following
%% include directives. yecc will otherwise assume that an old
%% yeccpre.hrl is included (one which defines yeccpars1/5).
yeccpars1(State1, State, States, Vstack, Token0, [Token | Tokens], Tzr) ->
yeccpars2(State, element(1, Token), [State1 | States],
[Token0 | Vstack], Token, Tokens, Tzr);
yeccpars1(State1, State, States, Vstack, Token0, [], {{_F,_A}, _Location}=Tzr) ->
yeccpars1([], Tzr, State, [State1 | States], [Token0 | Vstack]);
yeccpars1(State1, State, States, Vstack, Token0, [], {no_func, no_location}) ->
Location = yecctoken_end_location(Token0),
yeccpars2(State, '$end', [State1 | States], [Token0 | Vstack],
yecc_end(Location), [], {no_func, Location});
yeccpars1(State1, State, States, Vstack, Token0, [], {no_func, Location}) ->
yeccpars2(State, '$end', [State1 | States], [Token0 | Vstack],
yecc_end(Location), [], {no_func, Location}).
%% For internal use only.
yecc_end(Location) ->
{'$end', Location}.
yecctoken_end_location(Token) ->
try erl_anno:end_location(element(2, Token)) of
undefined -> yecctoken_location(Token);
Loc -> Loc
catch _:_ -> yecctoken_location(Token)
end.
-compile({nowarn_unused_function, yeccerror/1}).
yeccerror(Token) ->
Text = yecctoken_to_string(Token),
Location = yecctoken_location(Token),
{error, {Location, ?MODULE, ["syntax error before: ", Text]}}.
-compile({nowarn_unused_function, yecctoken_to_string/1}).
yecctoken_to_string(Token) ->
try erl_scan:text(Token) of
undefined -> yecctoken2string(Token);
Txt -> Txt
catch _:_ -> yecctoken2string(Token)
end.
yecctoken_location(Token) ->
try erl_scan:location(Token)
catch _:_ -> element(2, Token)
end.
-compile({nowarn_unused_function, yecctoken2string/1}).
yecctoken2string(Token) ->
try
yecctoken2string1(Token)
catch
_:_ ->
io_lib:format("~tp", [Token])
end.
-compile({nowarn_unused_function, yecctoken2string1/1}).
yecctoken2string1({atom, _, A}) -> io_lib:write_atom(A);
yecctoken2string1({integer,_,N}) -> io_lib:write(N);
yecctoken2string1({float,_,F}) -> io_lib:write(F);
yecctoken2string1({char,_,C}) -> io_lib:write_char(C);
yecctoken2string1({var,_,V}) -> io_lib:format("~s", [V]);
yecctoken2string1({string,_,S}) -> io_lib:write_string(S);
yecctoken2string1({reserved_symbol, _, A}) -> io_lib:write(A);
yecctoken2string1({_Cat, _, Val}) -> io_lib:format("~tp", [Val]);
yecctoken2string1({dot, _}) -> "'.'";
yecctoken2string1({'$end', _}) -> [];
yecctoken2string1({Other, _}) when is_atom(Other) ->
io_lib:write_atom(Other);
yecctoken2string1(Other) ->
io_lib:format("~tp", [Other]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
yeccpre.hrl
的代码被插入到 Yecc
生成的语法分析器中。
uni_parser.erl.skeleton
-module(uni_parser).
-export([parse/1, parse_and_scan/1, format_error/1]).
-file("uni_parser.yrl", 10). % `Erlang Code.` 所在行
% uni_parser.yrl 的 <Erlang Code>
-file("/opt/homebrew/Cellar/erlang/25.0_1/lib/erlang/lib/parsetools-2.4/include/yeccpre.hrl", 0).
% yeccpre.hrl 的代码
-file("uni_parser.erl", 184). % uni_parser.erl 当前行
% <Code>
-file("uni_parser.yrl", 4). % `Rootsymbol` 前一行
yeccpars2_0_(__Stack0) ->
[begin
{'__block__', [], []}
end | __Stack0].
-file("uni_parser.yrl", 0).
yeccpars2_2_(__Stack0) ->
[___1 | __Stack] = __Stack0,
[begin
'$undefined'
end | __Stack].
-file("uni_parser.yrl", 11). % uni_parser.erl 最后一行
Modules.EDoc
-file(File, Line).
% changing the pre-defined macros ?FILE and ?LINE
% to inform the compiler that the source program is generated by another tool
在 uni_parser.erl
中,定位 uni_parser.erl
, 以所在行为分际,理解 uni_parser.erl
代码。
重要的函数
(<0.81.0>) call uni_parser:parse([{any,1,"1"}])
(<0.81.0>) call uni_parser:yeccpars0([{any,1,"1"}],{no_func,no_location},0,[],[])
(<0.81.0>) call uni_parser:yeccpars1([{any,1,"1"}],{no_func,no_location},0,[],[])
(<0.81.0>) call uni_parser:yeccpars2(0,any,[],[],{any,1,"1"},[],{no_func,no_location})
(<0.81.0>) call uni_parser:yeccpars2_0(0,any,[],[],{any,1,"1"},[],{no_func,no_location})
(<0.81.0>) call uni_parser:yeccpars1(0,2,[],[],{any,1,"1"},[],{no_func,no_location})
(<0.81.0>) call uni_parser:yecctoken_end_location({any,1,"1"})
(<0.81.0>) call uni_parser:yecctoken_location({any,1,"1"})
(<0.81.0>) call uni_parser:yecc_end(1)
(<0.81.0>) call uni_parser:yeccpars2(2,'$end',[0],[{any,1,"1"}],{'$end',1},[],{no_func,1})
(<0.81.0>) call uni_parser:yeccpars2_2(2,'$end',[0],[{any,1,"1"}],{'$end',1},[],{no_func,1})
(<0.81.0>) call uni_parser:yeccgoto_grammer(0,'$end',[0],['$undefined'],{'$end',1},[],{no_func,1})
(<0.81.0>) call uni_parser:yeccpars2_1(1,'$end',[0],['$undefined'],{'$end',1},[],{no_func,1})
{ok,'$undefined'}
parse
是语法分析器的入口函数, 参数为 Token 列表。
yeccpars0
的主要作用是调用 yeccpars1
, 对调用结果做异常处理。
yeccpars0
的参数列表:
-
Tokens
-
Tzr
,初始为{no_func, no_location}
-
State
,初始为0
-
States
,初始为[]
-
Vstack
,初始为[]
yeccpars1
同 yeccpars0
一样, 也维护着 Tzr
, State
, States
, Vstack
等状态。
yeccpars1
有多个函数子句,用以处理以下情况:
-
有 Token 待处理
-
无 Token 待处理
-
处理 Token 和 State
yeccpars2
被 yeccpars1
调用,负责处理 Token。
yeccpars2.code
-dialyzer({nowarn_function, yeccpars2/7}).
-compile({nowarn_unused_function, yeccpars2/7}).
yeccpars2(0=S, Cat, Ss, Stack, T, Ts, Tzr) ->
yeccpars2_0(S, Cat, Ss, Stack, T, Ts, Tzr);
yeccpars2_0(S, 'any', Ss, Stack, T, Ts, Tzr) ->
yeccpars1(S, 2, Ss, Stack, T, Ts, Tzr);
注意 yeccpars1
被 yeccpars2_*
调用的情况。