erlydtl_preparser.hrl 7.6 KB

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