erlydtl_preparser.hrl 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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:function_clause = Error ->
  63. case erlang:get_stacktrace() of
  64. %% [{atom() | tuple(),atom(),[any()] | byte(),[{'file',string()} | {'line',pos_integer()}]}]
  65. [{?MODULE, F, ArityOrArgs, _Source}|_]=Stacktrace ->
  66. erlang:raise(
  67. error,
  68. yecc_error_type(Error, F, ArityOrArgs),
  69. Stacktrace);
  70. Stacktrace ->
  71. erlang:raise(error, Error, Stacktrace)
  72. end;
  73. %% Probably thrown from return_error/2:
  74. throw: {error, {_Line, ?MODULE, _M}} = Error ->
  75. Error
  76. end.
  77. yecc_error_type(function_clause=Error, F, ArityOrArgs) ->
  78. case atom_to_list(F) of
  79. "yeccgoto_" ++ SymbolL ->
  80. {ok,[{atom,_,Symbol}],_} = erl_scan:string(SymbolL),
  81. State = case ArityOrArgs of
  82. [S,_,_,_,_,_,_] -> S;
  83. _ -> state_is_unknown
  84. end,
  85. Desc = {Symbol, State, missing_in_goto_table},
  86. {yecc_bug, ?CODE_VERSION, Desc};
  87. _ -> Error
  88. end.
  89. -define(checkparse(CALL, STATE),
  90. try case CALL of
  91. {error, Error} ->
  92. {error, Error, STATE};
  93. Else ->
  94. Else
  95. end
  96. catch
  97. throw: return_state ->
  98. {ok, STATE}
  99. end).
  100. yeccpars1([Token | Tokens], Tzr, State, States, Vstack) ->
  101. ?checkparse(
  102. yeccpars2(State, element(1, Token), States, Vstack, Token, Tokens, Tzr),
  103. [[Token|Tokens], Tzr, State, States, Vstack]
  104. );
  105. yeccpars1([], {{F, A},_Line}, State, States, Vstack) ->
  106. case apply(F, A) of
  107. {ok, Tokens, Endline} ->
  108. yeccpars1(Tokens, {{F, A}, Endline}, State, States, Vstack);
  109. {eof, Endline} ->
  110. yeccpars1([], {no_func, Endline}, State, States, Vstack);
  111. {error, Descriptor, _Endline} ->
  112. {error, Descriptor}
  113. end;
  114. yeccpars1([], {no_func, no_line}, State, States, Vstack) ->
  115. Line = 999999,
  116. yeccpars2(State, '$end', States, Vstack, yecc_end(Line), [],
  117. {no_func, Line});
  118. yeccpars1([], {no_func, Endline}, State, States, Vstack) ->
  119. yeccpars2(State, '$end', States, Vstack, yecc_end(Endline), [],
  120. {no_func, Endline}).
  121. %% yeccpars1/7 is called from generated code.
  122. %%
  123. %% When using the {includefile, Includefile} option, make sure that
  124. %% yeccpars1/7 can be found by parsing the file without following
  125. %% include directives. yecc will otherwise assume that an old
  126. %% yeccpre.hrl is included (one which defines yeccpars1/5).
  127. yeccpars1(State1, State, States, Vstack, Token0, [Token | Tokens], Tzr) ->
  128. ?checkparse(
  129. yeccpars2(State, element(1, Token), [State1 | States],
  130. [Token0 | Vstack], Token, Tokens, Tzr),
  131. [[Token0, Token | Tokens], Tzr, State1, States, Vstack]
  132. );
  133. yeccpars1(State1, State, States, Vstack, Token0, [], {{_F,_A}, _Line}=Tzr) ->
  134. yeccpars1([], Tzr, State, [State1 | States], [Token0 | Vstack]);
  135. yeccpars1(State1, State, States, Vstack, Token0, [], {no_func, no_line}) ->
  136. Line = yecctoken_end_location(Token0),
  137. yeccpars2(State, '$end', [State1 | States], [Token0 | Vstack],
  138. yecc_end(Line), [], {no_func, Line});
  139. yeccpars1(State1, State, States, Vstack, Token0, [], {no_func, Line}) ->
  140. yeccpars2(State, '$end', [State1 | States], [Token0 | Vstack],
  141. yecc_end(Line), [], {no_func, Line}).
  142. %% For internal use only.
  143. yecc_end({Line,_Column}) ->
  144. {'$end', Line};
  145. yecc_end(Line) ->
  146. {'$end', Line}.
  147. yecctoken_end_location(Token) ->
  148. try
  149. {text, Str} = erl_scan:token_info(Token, text),
  150. {line, Line} = erl_scan:token_info(Token, line),
  151. Parts = re:split(Str, "\n"),
  152. Dline = length(Parts) - 1,
  153. Yline = Line + Dline,
  154. case erl_scan:token_info(Token, column) of
  155. {column, Column} ->
  156. Col = byte_size(lists:last(Parts)),
  157. {Yline, Col + if Dline =:= 0 -> Column; true -> 1 end};
  158. undefined ->
  159. Yline
  160. end
  161. catch _:_ ->
  162. yecctoken_location(Token)
  163. end.
  164. -compile({nowarn_unused_function, yeccerror/1}).
  165. yeccerror(Token) ->
  166. Text = yecctoken_to_string(Token),
  167. Location = yecctoken_location(Token),
  168. {error, {Location, ?MODULE, ["syntax error before: ", Text]}}.
  169. -compile({nowarn_unused_function, yecctoken_to_string/1}).
  170. yecctoken_to_string(Token) ->
  171. case catch erl_scan:token_info(Token, text) of
  172. {text, Txt} -> Txt;
  173. _ -> yecctoken2string(Token)
  174. end.
  175. yecctoken_location(Token) ->
  176. case catch erl_scan:token_info(Token, location) of
  177. {location, Loc} -> Loc;
  178. _ -> element(2, Token)
  179. end.
  180. -compile({nowarn_unused_function, yecctoken2string/1}).
  181. yecctoken2string({atom, _, A}) -> io_lib:write(A);
  182. yecctoken2string({integer,_,N}) -> io_lib:write(N);
  183. yecctoken2string({float,_,F}) -> io_lib:write(F);
  184. yecctoken2string({char,_,C}) -> io_lib:write_char(C);
  185. yecctoken2string({var,_,V}) -> io_lib:format("~s", [V]);
  186. yecctoken2string({string,_,S}) -> io_lib:write_unicode_string(S);
  187. yecctoken2string({reserved_symbol, _, A}) -> io_lib:write(A);
  188. yecctoken2string({_Cat, _, Val}) -> io_lib:format("~p",[Val]);
  189. yecctoken2string({dot, _}) -> "'.'";
  190. yecctoken2string({'$end', _}) ->
  191. [];
  192. yecctoken2string({Other, _}) when is_atom(Other) ->
  193. io_lib:write(Other);
  194. yecctoken2string(Other) ->
  195. io_lib:write(Other).
  196. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%