Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 215 lines (179 sloc) 8.43 KB
%% The source of this file is part of leex distribution, as such it
%% has the same Copyright as the other files in the leex
%% distribution. The Copyright is defined in the accompanying file
%% COPYRIGHT. However, the resultant scanner generated by leex is the
%% property of the creator of the scanner and is not covered by that
%% Copyright.
##module
-export([string/1,string/2,token/2,token/3,tokens/2,tokens/3]).
-export([format_error/1]).
%% User code. This is placed here to allow extra attributes.
##code
format_error({illegal,S}) -> ["illegal characters ",io_lib:write_string(S)];
format_error({user,S}) -> S.
string(String) -> string(String, 1).
string(String, Line) -> string(String, Line, String, []).
%% string(InChars, Line, TokenChars, Tokens) ->
%% {ok,Tokens,Line} | {error,ErrorInfo,Line}.
string([], L, [], Ts) -> %No partial tokens!
{ok,yyrev(Ts),L};
string(Ics0, L0, Tcs, Ts) ->
case yystate(yystate(), Ics0, L0, 0, reject, 0) of
{A,Alen,Ics1,L1} -> %Accepting end state
string_cont(Ics1, L1, yyaction(A, Alen, Tcs, L1), Ts);
{A,Alen,Ics1,L1,_S1} -> %After an accepting state
string_cont(Ics1, L1, yyaction(A, Alen, Tcs, L1), Ts);
{reject,_Alen,Clen,_Ics1,L1,_S1} -> %After a non-accepting state
{error,{L1,?MODULE,{illegal,yypre(Tcs, Clen+1)}},L1};
{A,Alen,_Clen,_Ics1,L1,_S1} ->
string_cont(yysuf(Tcs, Alen), L1, yyaction(A, Alen, Tcs, L1), Ts)
end.
%% string_cont(RestChars, Line, Token, Tokens)
%% Test for and remove the end token wrapper.
string_cont(Rest, Line, {token,T}, Ts) ->
string(Rest, Line, Rest, [T|Ts]);
string_cont(Rest, Line, {end_token,T}, Ts) ->
string(Rest, Line, Rest, [T|Ts]);
string_cont(Rest, Line, skip_token, Ts) ->
string(Rest, Line, Rest, Ts);
string_cont(_Rest, Line, {error,S}, _Ts) ->
{error,{Line,?MODULE,{user,S}},Line}.
%% token(Continuation, Chars, Line) ->
%% {more,Continuation} | {done,ReturnVal,RestChars}.
%% Must be careful when re-entering to append the latest characters to the
%% after characters in an accept.
token(Cont, Chars) -> token(Cont, Chars, 1).
token([], Chars, Line) ->
token(Chars, Line, yystate(), Chars, 0, reject, 0);
token({token,Line,State,Tcs,Clen,Action,Alen}, Chars, _) ->
token(Chars, Line, State, Tcs ++ Chars, Clen, Action, Alen).
%% token(InChars, Line, State, TokenChars, CurrTokLen,
%% AcceptAction, AcceptLen) ->
%% {more,Continuation} | {done,ReturnVal,RestChars}.
token(Ics0, L0, S0, Tcs, Clen0, A0, Alen0) ->
case yystate(S0, Ics0, L0, Clen0, A0, Alen0) of
{A1,Alen1,Ics1,L1} -> %Accepting end state
token_cont(Ics1, L1, yyaction(A1, Alen1, Tcs, L1));
{A1,Alen1,[],L1,S1} -> %After an accepting state
{more,{token,L1,S1,Tcs,Alen1,A1,Alen1}};
{A1,Alen1,Ics1,L1,_S1} ->
token_cont(Ics1, L1, yyaction(A1, Alen1, Tcs, L1));
{A1,Alen1,Clen1,[],L1,S1} -> %After a non-accepting state
{more,{token,L1,S1,Tcs,Clen1,A1,Alen1}};
{reject,_Alen1,_Clen1,eof,L1,_S1} ->
{done,{eof,L1},[]};
{reject,_Alen1,Clen1,Ics1,L1,_S1} ->
{done,{error,{L1,?MODULE,{illegal,yypre(Tcs, Clen1+1)}},L1},Ics1};
{A1,Alen1,_Clen1,_Ics1,L1,_S1} ->
token_cont(yysuf(Tcs, Alen1), L1, yyaction(A1, Alen1, Tcs, L1))
end.
%% tokens_cont(RestChars, Line, Token)
%% If we have a token or error then return done, else if we have a
%% skip_token then continue.
token_cont(Rest, Line, {token,T}) ->
{done,{ok,T,Line},Rest};
token_cont(Rest, Line, {end_token,T}) ->
{done,{ok,T,Line},Rest};
token_cont(Rest, Line, skip_token) ->
token(Rest, Line, yystate(), Rest, 0, reject, 0);
token_cont(Rest, Line, {error,S}) ->
{done,{error,{Line,?MODULE,{user,S}},Line},Rest}.
%% tokens(Continuation, Chars, Line) ->
%% {more,Continuation} | {done,ReturnVal,RestChars}.
%% Must be careful when re-entering to append the latest characters to the
%% after characters in an accept.
tokens(Cont, Chars) -> tokens(Cont, Chars, 1).
tokens([], Chars, Line) ->
tokens(Chars, Line, yystate(), Chars, 0, [], reject, 0);
tokens({tokens,Line,State,Tcs,Clen,Ts,Action,Alen}, Chars, _) ->
tokens(Chars, Line, State, Tcs ++ Chars, Clen, Ts, Action, Alen);
tokens({skip_tokens,Line,State,Tcs,Clen,Error,Action,Alen}, Chars, _) ->
skip_tokens(Chars, Line, State, Tcs ++ Chars, Clen, Error, Action, Alen).
%% tokens(InChars, Line, State, TokenChars, CurrTokLen, Tokens,
%% AcceptAction, AcceptLen) ->
%% {more,Continuation} | {done,ReturnVal,RestChars}.
tokens(Ics0, L0, S0, Tcs, Clen0, Ts, A0, Alen0) ->
case yystate(S0, Ics0, L0, Clen0, A0, Alen0) of
{A1,Alen1,Ics1,L1} -> %Accepting end state
tokens_cont(Ics1, L1, yyaction(A1, Alen1, Tcs, L1), Ts);
{A1,Alen1,[],L1,S1} -> %After an accepting state
{more,{tokens,L1,S1,Tcs,Alen1,Ts,A1,Alen1}};
{A1,Alen1,Ics1,L1,_S1} ->
tokens_cont(Ics1, L1, yyaction(A1, Alen1, Tcs, L1), Ts);
{A1,Alen1,Clen1,[],L1,S1} -> %After a non-accepting state
{more,{tokens,L1,S1,Tcs,Clen1,Ts,A1,Alen1}};
{reject,_Alen1,_Clen1,eof,L1,_S1} ->
{done,if Ts == [] -> {eof,L1};
true -> {ok,yyrev(Ts),L1} end,[]};
{reject,_Alen1,Clen1,_Ics1,L1,_S1} ->
%% Skip rest of tokens.
skip_tokens(yysuf(Tcs, Clen1+1), L1,
{L1,?MODULE,{illegal,yypre(Tcs, Clen1+1)}});
{A1,Alen1,_Clen1,_Ics1,L1,_S1} ->
tokens_cont(yysuf(Tcs, Alen1), L1, yyaction(A1, Alen1, Tcs, L1), Ts)
end.
%% tokens_cont(RestChars, Line, Token, Tokens)
%% If we have a end_token or error then return done, else if we have
%% a token then save it and continue, else if we have a skip_token
%% just continue.
tokens_cont(Rest, Line, {token,T}, Ts) ->
tokens(Rest, Line, yystate(), Rest, 0, [T|Ts], reject, 0);
tokens_cont(Rest, Line, {end_token,T}, Ts) ->
{done,{ok,yyrev(Ts, [T]),Line},Rest};
tokens_cont(Rest, Line, skip_token, Ts) ->
tokens(Rest, Line, yystate(), Rest, 0, Ts, reject, 0);
tokens_cont(Rest, Line, {error,S}, _Ts) ->
skip_tokens(Rest, Line, {Line,?MODULE,{user,S}}).
%%skip_tokens(InChars, Line, Error) -> {done,{error,Error,Line},Ics}.
%% Skip tokens until an end token, junk everything and return the error.
skip_tokens(Ics, Line, Error) ->
skip_tokens(Ics, Line, yystate(), Ics, 0, Error, reject, 0).
%% skip_tokens(InChars, Line, State, TokenChars, CurrTokLen, Tokens,
%% AcceptAction, AcceptLen) ->
%% {more,Continuation} | {done,ReturnVal,RestChars}.
skip_tokens(Ics0, L0, S0, Tcs, Clen0, Error, A0, Alen0) ->
case yystate(S0, Ics0, L0, Clen0, A0, Alen0) of
{A1,Alen1,Ics1,L1} -> %Accepting end state
skip_cont(Ics1, L1, yyaction(A1, Alen1, Tcs, L1), Error);
{A1,Alen1,[],L1,S1} -> %After an accepting state
{more,{skip_tokens,L1,S1,Tcs,Alen1,Error,A1,Alen1}};
{A1,Alen1,Ics1,L1,_S1} ->
skip_cont(Ics1, L1, yyaction(A1, Alen1, Tcs, L1), Error);
{A1,Alen1,Clen1,[],L1,S1} -> %After a non-accepting state
{more,{skip_tokens,L1,S1,Tcs,Clen1,Error,A1,Alen1}};
{reject,_Alen1,_Clen1,eof,L1,_S1} ->
{done,{error,Error,L1},[]};
{reject,_Alen1,Clen1,_Ics1,L1,_S1} ->
skip_tokens(yysuf(Tcs, Clen1+1), L1, Error);
{A1,Alen1,_Clen1,_Ics1,L1,_S1} ->
skip_cont(yysuf(Tcs, Alen1), L1, yyaction(A1, Alen1, Tcs, L1), Error)
end.
%% skip_cont(RestChars, Line, Token, Error)
%% Skip tokens until we have an end_token or error then return done
%% with the original rror.
skip_cont(Rest, Line, {token,_T}, Error) ->
skip_tokens(Rest, Line, yystate(), Rest, 0, Error, reject, 0);
skip_cont(Rest, Line, {end_token,_T}, Error) ->
{done,{error,Error,Line},Rest};
skip_cont(Rest, Line, {error,_S}, Error) ->
skip_tokens(Rest, Line, yystate(), Rest, 0, Error, reject, 0);
skip_cont(Rest, Line, skip_token, Error) ->
skip_tokens(Rest, Line, yystate(), Rest, 0, Error, reject, 0).
yyrev(List) -> lists:reverse(List).
yyrev(List, Tail) -> lists:reverse(List, Tail).
yypre(List, N) -> lists:sublist(List, N).
yysuf(List, N) -> lists:nthtail(N, List).
%% yystate() -> InitialState.
%% yystate(State, InChars, Line, CurrTokLen, AcceptAction, AcceptLen) ->
%% {Action, AcceptLen, RestChars, Line} |
%% {Action, AcceptLen, RestChars, Line, State} |
%% {reject, AcceptLen, CurrTokLen, RestChars, Line, State} |
%% {Action, AcceptLen, CurrTokLen, RestChars, Line, State}.
%% Generated state transition functions. The non-accepting end state
%% return signal either an unrecognised character or end of current
%% input.
##dfa
%% yyaction(Action, TokenLength, TokenChars, TokenLine) ->
%% {token,Token} | {end_token, Token} | skip_token | {error,String}.
%% Generated action function.
##actions