erlydtl_preparser.hrl 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. %% -*- mode: erlang -*-
  2. %% vim: syntax=erlang
  3. %% This file is based on parsetools/include/yeccpre.hrl
  4. %%
  5. %% The applied modifiactions are to enable the caller to recover
  6. %% after a parse error, and then resume normal parsing.
  7. %%
  8. %% %CopyrightBegin%
  9. %%
  10. %% Copyright Ericsson AB 1996-2015. All Rights Reserved.
  11. %%
  12. %% Licensed under the Apache License, Version 2.0 (the "License");
  13. %% you may not use this file except in compliance with the License.
  14. %% You may obtain a copy of the License at
  15. %%
  16. %% http://www.apache.org/licenses/LICENSE-2.0
  17. %%
  18. %% Unless required by applicable law or agreed to in writing, software
  19. %% distributed under the License is distributed on an "AS IS" BASIS,
  20. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. %% See the License for the specific language governing permissions and
  22. %% limitations under the License.
  23. %%
  24. %% %CopyrightEnd%
  25. %%
  26. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  27. % The parser generator will insert appropriate declarations before this line.%
  28. -export([parse/1, parse_and_scan/1, format_error/1, resume/1]).
  29. -type yecc_ret() :: {'error', _, _} | {'ok', _}.
  30. -spec parse(Tokens :: list()) -> yecc_ret().
  31. parse(Tokens) ->
  32. yeccpars0(Tokens, {no_func, no_line}, 0, [], []).
  33. -spec parse_and_scan({function() | {atom(), atom()}, [_]}
  34. | {atom(), atom(), [_]}) -> yecc_ret().
  35. parse_and_scan({F, A}) ->
  36. yeccpars0([], {{F, A}, no_line}, 0, [], []);
  37. parse_and_scan({M, F, A}) ->
  38. Arity = length(A),
  39. yeccpars0([], {{fun M:F/Arity, A}, no_line}, 0, [], []).
  40. resume([Tokens, Tzr, State, States, Vstack]) ->
  41. yeccpars0(Tokens, Tzr, State, States, Vstack).
  42. -spec format_error(any()) -> [char() | list()].
  43. format_error(Message) ->
  44. case io_lib:deep_char_list(Message) of
  45. true ->
  46. Message;
  47. _ ->
  48. io_lib:write(Message)
  49. end.
  50. %% To be used in grammar files to throw an error message to the parser
  51. %% toplevel. Doesn't have to be exported!
  52. -compile({nowarn_unused_function, return_error/2}).
  53. -spec return_error(integer(), any()) -> no_return().
  54. return_error(Line, Message) ->
  55. throw({error, {Line, ?MODULE, Message}}).
  56. -compile({nowarn_unused_function, return_state/0}).
  57. return_state() ->
  58. throw(return_state).
  59. -define(CODE_VERSION, "1.4").
  60. yeccpars0(Tokens, Tzr, State, States, Vstack) ->
  61. try yeccpars1(Tokens, Tzr, State, States, Vstack)
  62. catch
  63. error: Error ->
  64. Stacktrace = erlang:get_stacktrace(),
  65. try yecc_error_type(Error, Stacktrace) of
  66. Desc ->
  67. erlang:raise(error, {yecc_bug, ?CODE_VERSION, Desc},
  68. Stacktrace)
  69. catch _:_ -> erlang:raise(error, Error, Stacktrace)
  70. end;
  71. %% Probably thrown from return_error/2:
  72. throw: {error, {_Line, ?MODULE, _M}} = Error ->
  73. Error
  74. end.
  75. yecc_error_type(function_clause, [{?MODULE,F,ArityOrArgs,_} | _]) ->
  76. case atom_to_list(F) of
  77. "yeccgoto_" ++ SymbolL ->
  78. {ok,[{atom,_,Symbol}],_} = erl_scan:string(SymbolL),
  79. State = case ArityOrArgs of
  80. [S,_,_,_,_,_,_] -> S;
  81. _ -> state_is_unknown
  82. end,
  83. {Symbol, State, missing_in_goto_table}
  84. end.
  85. -define(checkparse(CALL, STATE),
  86. try case CALL of
  87. {error, Error} ->
  88. {error, Error, STATE};
  89. Else ->
  90. Else
  91. end
  92. catch
  93. throw: return_state ->
  94. {ok, STATE}
  95. end).
  96. yeccpars1([Token | Tokens], Tzr, State, States, Vstack) ->
  97. ?checkparse(
  98. yeccpars2(State, element(1, Token), States, Vstack, Token, Tokens, Tzr),
  99. [[Token|Tokens], Tzr, State, States, Vstack]
  100. );
  101. yeccpars1([], {{F, A},_Line}, State, States, Vstack) ->
  102. case apply(F, A) of
  103. {ok, Tokens, Endline} ->
  104. yeccpars1(Tokens, {{F, A}, Endline}, State, States, Vstack);
  105. {eof, Endline} ->
  106. yeccpars1([], {no_func, Endline}, State, States, Vstack);
  107. {error, Descriptor, _Endline} ->
  108. {error, Descriptor}
  109. end;
  110. yeccpars1([], {no_func, no_line}, State, States, Vstack) ->
  111. Line = 999999,
  112. yeccpars2(State, '$end', States, Vstack, yecc_end(Line), [],
  113. {no_func, Line});
  114. yeccpars1([], {no_func, Endline}, State, States, Vstack) ->
  115. yeccpars2(State, '$end', States, Vstack, yecc_end(Endline), [],
  116. {no_func, Endline}).
  117. %% yeccpars1/7 is called from generated code.
  118. %%
  119. %% When using the {includefile, Includefile} option, make sure that
  120. %% yeccpars1/7 can be found by parsing the file without following
  121. %% include directives. yecc will otherwise assume that an old
  122. %% yeccpre.hrl is included (one which defines yeccpars1/5).
  123. yeccpars1(State1, State, States, Vstack, Token0, [Token | Tokens], Tzr) ->
  124. ?checkparse(
  125. yeccpars2(State, element(1, Token), [State1 | States],
  126. [Token0 | Vstack], Token, Tokens, Tzr),
  127. [[Token0, Token | Tokens], Tzr, State1, States, Vstack]
  128. );
  129. yeccpars1(State1, State, States, Vstack, Token0, [], {{_F,_A}, _Line}=Tzr) ->
  130. yeccpars1([], Tzr, State, [State1 | States], [Token0 | Vstack]);
  131. yeccpars1(State1, State, States, Vstack, Token0, [], {no_func, no_line}) ->
  132. Line = yecctoken_end_location(Token0),
  133. yeccpars2(State, '$end', [State1 | States], [Token0 | Vstack],
  134. yecc_end(Line), [], {no_func, Line});
  135. yeccpars1(State1, State, States, Vstack, Token0, [], {no_func, Line}) ->
  136. yeccpars2(State, '$end', [State1 | States], [Token0 | Vstack],
  137. yecc_end(Line), [], {no_func, Line}).
  138. %% For internal use only.
  139. yecc_end({Line,_Column}) ->
  140. {'$end', Line};
  141. yecc_end(Line) ->
  142. {'$end', Line}.
  143. yecctoken_end_location(Token) ->
  144. try erl_anno:end_location(element(2, Token)) of
  145. undefined -> yecctoken_location(Token);
  146. Loc -> Loc
  147. catch _:_ -> yecctoken_location(Token)
  148. end.
  149. -compile({nowarn_unused_function, yeccerror/1}).
  150. yeccerror(Token) ->
  151. Text = yecctoken_to_string(Token),
  152. Location = yecctoken_location(Token),
  153. {error, {Location, ?MODULE, ["syntax error before: ", Text]}}.
  154. -compile({nowarn_unused_function, yecctoken_to_string/1}).
  155. yecctoken_to_string(Token) ->
  156. try erl_scan:text(Token) of
  157. undefined -> yecctoken2string(Token);
  158. Txt -> Txt
  159. catch _:_ -> yecctoken2string(Token)
  160. end.
  161. yecctoken_location(Token) ->
  162. try erl_scan:location(Token)
  163. catch _:_ -> element(2, Token)
  164. end.
  165. -compile({nowarn_unused_function, yecctoken2string/1}).
  166. yecctoken2string({atom, _, A}) -> io_lib:write(A);
  167. yecctoken2string({integer,_,N}) -> io_lib:write(N);
  168. yecctoken2string({float,_,F}) -> io_lib:write(F);
  169. yecctoken2string({char,_,C}) -> io_lib:write_char(C);
  170. yecctoken2string({var,_,V}) -> io_lib:format("~s", [V]);
  171. yecctoken2string({string,_,S}) -> io_lib:write_string(S);
  172. yecctoken2string({reserved_symbol, _, A}) -> io_lib:write(A);
  173. yecctoken2string({_Cat, _, Val}) -> io_lib:format("~p",[Val]);
  174. yecctoken2string({dot, _}) -> "'.'";
  175. yecctoken2string({'$end', _}) ->
  176. [];
  177. yecctoken2string({Other, _}) when is_atom(Other) ->
  178. io_lib:write(Other);
  179. yecctoken2string(Other) ->
  180. io_lib:write(Other).
  181. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%