| 1 |
-file("peg_includes.hrl", 1). |
| 2 |
-type index() :: {{line, pos_integer()}, {column, pos_integer()}}. |
| 3 |
-type input() :: binary(). |
| 4 |
-type parse_failure() :: {fail, term()}. |
| 5 |
-type parse_success() :: {term(), input(), index()}. |
| 6 |
-type parse_result() :: parse_failure() | parse_success(). |
| 7 |
-type parse_fun() :: fun((input(), index()) -> parse_result()). |
| 8 |
-type xform_fun() :: fun((input(), index()) -> term()). |
| 9 |
|
| 10 |
-spec p(input(), index(), atom(), parse_fun(), xform_fun()) -> parse_result(). |
| 11 |
p(Inp, StartIndex, Name, ParseFun, TransformFun) -> |
| 12 |
case get_memo(StartIndex, Name) of % See if the current reduction is memoized |
| 13 |
{ok, Memo} -> %Memo; % If it is, return the stored result |
| 14 |
Memo; |
| 15 |
_ -> % If not, attempt to parse |
| 16 |
Result = case ParseFun(Inp, StartIndex) of |
| 17 |
{fail,_} = Failure -> % If it fails, memoize the failure |
| 18 |
Failure; |
| 19 |
{Match, InpRem, NewIndex} -> % If it passes, transform and memoize the result. |
| 20 |
Transformed = TransformFun(Match, StartIndex), |
| 21 |
{Transformed, InpRem, NewIndex} |
| 22 |
end, |
| 23 |
memoize(StartIndex, Name, Result), |
| 24 |
Result |
| 25 |
end. |
| 26 |
|
| 27 |
-spec setup_memo() -> ets:tid(). |
| 28 |
setup_memo() -> |
| 29 |
put({parse_memo_table, ?MODULE}, ets:new(?MODULE, [set])). |
| 30 |
|
| 31 |
-spec release_memo() -> true. |
| 32 |
release_memo() -> |
| 33 |
ets:delete(memo_table_name()). |
| 34 |
|
| 35 |
-spec memoize(index(), atom(), parse_result()) -> true. |
| 36 |
memoize(Index, Name, Result) -> |
| 37 |
Memo = case ets:lookup(memo_table_name(), Index) of |
| 38 |
[] -> []; |
| 39 |
[{Index, Plist}] -> Plist |
| 40 |
end, |
| 41 |
ets:insert(memo_table_name(), {Index, [{Name, Result}|Memo]}). |
| 42 |
|
| 43 |
-spec get_memo(index(), atom()) -> {ok, term()} | {error, not_found}. |
| 44 |
get_memo(Index, Name) -> |
| 45 |
case ets:lookup(memo_table_name(), Index) of |
| 46 |
[] -> {error, not_found}; |
| 47 |
[{Index, Plist}] -> |
| 48 |
case proplists:lookup(Name, Plist) of |
| 49 |
{Name, Result} -> {ok, Result}; |
| 50 |
_ -> {error, not_found} |
| 51 |
end |
| 52 |
end. |
| 53 |
|
| 54 |
-spec memo_table_name() -> ets:tid(). |
| 55 |
memo_table_name() -> |
| 56 |
get({parse_memo_table, ?MODULE}). |
| 57 |
|
| 58 |
-ifdef(p_eof). |
| 59 |
-spec p_eof() -> parse_fun(). |
| 60 |
p_eof() -> |
| 61 |
fun(<<>>, Index) -> {eof, [], Index}; |
| 62 |
(_, Index) -> {fail, {expected, eof, Index}} end. |
| 63 |
-endif. |
| 64 |
|
| 65 |
-ifdef(p_optional). |
| 66 |
-spec p_optional(parse_fun()) -> parse_fun(). |
| 67 |
p_optional(P) -> |
| 68 |
fun(Input, Index) -> |
| 69 |
case P(Input, Index) of |
| 70 |
{fail,_} -> {[], Input, Index}; |
| 71 |
{_, _, _} = Success -> Success |
| 72 |
end |
| 73 |
end. |
| 74 |
-endif. |
| 75 |
|
| 76 |
-ifdef(p_not). |
| 77 |
-spec p_not(parse_fun()) -> parse_fun(). |
| 78 |
p_not(P) -> |
| 79 |
fun(Input, Index)-> |
| 80 |
case P(Input,Index) of |
| 81 |
{fail,_} -> |
| 82 |
{[], Input, Index}; |
| 83 |
{Result, _, _} -> {fail, {expected, {no_match, Result},Index}} |
| 84 |
end |
| 85 |
end. |
| 86 |
-endif. |
| 87 |
|
| 88 |
-ifdef(p_assert). |
| 89 |
-spec p_assert(parse_fun()) -> parse_fun(). |
| 90 |
p_assert(P) -> |
| 91 |
fun(Input,Index) -> |
| 92 |
case P(Input,Index) of |
| 93 |
{fail,_} = Failure-> Failure; |
| 94 |
_ -> {[], Input, Index} |
| 95 |
end |
| 96 |
end. |
| 97 |
-endif. |
| 98 |
|
| 99 |
-ifdef(p_seq). |
| 100 |
-spec p_seq([parse_fun()]) -> parse_fun(). |
| 101 |
p_seq(P) -> |
| 102 |
fun(Input, Index) -> |
| 103 |
p_all(P, Input, Index, []) |
| 104 |
end. |
| 105 |
|
| 106 |
-spec p_all([parse_fun()], input(), index(), [term()]) -> parse_result(). |
| 107 |
p_all([], Inp, Index, Accum ) -> {lists:reverse( Accum ), Inp, Index}; |
| 108 |
p_all([P|Parsers], Inp, Index, Accum) -> |
| 109 |
case P(Inp, Index) of |
| 110 |
{fail, _} = Failure -> Failure; |
| 111 |
{Result, InpRem, NewIndex} -> p_all(Parsers, InpRem, NewIndex, [Result|Accum]) |
| 112 |
end. |
| 113 |
-endif. |
| 114 |
|
| 115 |
-ifdef(p_choose). |
| 116 |
-spec p_choose([parse_fun()]) -> parse_fun(). |
| 117 |
p_choose(Parsers) -> |
| 118 |
fun(Input, Index) -> |
| 119 |
p_attempt(Parsers, Input, Index, none) |
| 120 |
end. |
| 121 |
|
| 122 |
-spec p_attempt([parse_fun()], input(), index(), none | parse_failure()) -> parse_result(). |
| 123 |
p_attempt([], _Input, _Index, Failure) -> Failure; |
| 124 |
p_attempt([P|Parsers], Input, Index, FirstFailure)-> |
| 125 |
case P(Input, Index) of |
| 126 |
{fail, _} = Failure -> |
| 127 |
case FirstFailure of |
| 128 |
none -> p_attempt(Parsers, Input, Index, Failure); |
| 129 |
_ -> p_attempt(Parsers, Input, Index, FirstFailure) |
| 130 |
end; |
| 131 |
Result -> Result |
| 132 |
end. |
| 133 |
-endif. |
| 134 |
|
| 135 |
-ifdef(p_zero_or_more). |
| 136 |
-spec p_zero_or_more(parse_fun()) -> parse_fun(). |
| 137 |
p_zero_or_more(P) -> |
| 138 |
fun(Input, Index) -> |
| 139 |
p_scan(P, Input, Index, []) |
| 140 |
end. |
| 141 |
-endif. |
| 142 |
|
| 143 |
-ifdef(p_one_or_more). |
| 144 |
-spec p_one_or_more(parse_fun()) -> parse_fun(). |
| 145 |
p_one_or_more(P) -> |
| 146 |
fun(Input, Index)-> |
| 147 |
Result = p_scan(P, Input, Index, []), |
| 148 |
case Result of |
| 149 |
{[_|_], _, _} -> |
| 150 |
Result; |
| 151 |
_ -> |
| 152 |
{fail, {expected, Failure, _}} = P(Input,Index), |
| 153 |
{fail, {expected, {at_least_one, Failure}, Index}} |
| 154 |
end |
| 155 |
end. |
| 156 |
-endif. |
| 157 |
|
| 158 |
-ifdef(p_label). |
| 159 |
-spec p_label(atom(), parse_fun()) -> parse_fun(). |
| 160 |
p_label(Tag, P) -> |
| 161 |
fun(Input, Index) -> |
| 162 |
case P(Input, Index) of |
| 163 |
{fail,_} = Failure -> |
| 164 |
Failure; |
| 165 |
{Result, InpRem, NewIndex} -> |
| 166 |
{{Tag, Result}, InpRem, NewIndex} |
| 167 |
end |
| 168 |
end. |
| 169 |
-endif. |
| 170 |
|
| 171 |
-ifdef(p_scan). |
| 172 |
-spec p_scan(parse_fun(), input(), index(), [term()]) -> {[term()], input(), index()}. |
| 173 |
p_scan(_, <<>>, Index, Accum) -> {lists:reverse(Accum), <<>>, Index}; |
| 174 |
p_scan(P, Inp, Index, Accum) -> |
| 175 |
case P(Inp, Index) of |
| 176 |
{fail,_} -> {lists:reverse(Accum), Inp, Index}; |
| 177 |
{Result, InpRem, NewIndex} -> p_scan(P, InpRem, NewIndex, [Result | Accum]) |
| 178 |
end. |
| 179 |
-endif. |
| 180 |
|
| 181 |
-ifdef(p_string). |
| 182 |
-spec p_string(binary()) -> parse_fun(). |
| 183 |
p_string(S) -> |
| 184 |
Length = erlang:byte_size(S), |
| 185 |
fun(Input, Index) -> |
| 186 |
try |
| 187 |
<<S:Length/binary, Rest/binary>> = Input, |
| 188 |
{S, Rest, p_advance_index(S, Index)} |
| 189 |
catch |
| 190 |
error:{badmatch,_} -> {fail, {expected, {string, S}, Index}} |
| 191 |
end |
| 192 |
end. |
| 193 |
-endif. |
| 194 |
|
| 195 |
-ifdef(p_anything). |
| 196 |
-spec p_anything() -> parse_fun(). |
| 197 |
p_anything() -> |
| 198 |
fun(<<>>, Index) -> {fail, {expected, any_character, Index}}; |
| 199 |
(Input, Index) when is_binary(Input) -> |
| 200 |
<<C/utf8, Rest/binary>> = Input, |
| 201 |
{<<C/utf8>>, Rest, p_advance_index(<<C/utf8>>, Index)} |
| 202 |
end. |
| 203 |
-endif. |
| 204 |
|
| 205 |
-ifdef(p_charclass). |
| 206 |
-spec p_charclass(string() | binary()) -> parse_fun(). |
| 207 |
p_charclass(Class) -> |
| 208 |
{ok, RE} = re:compile(Class, [unicode, dotall]), |
| 209 |
fun(Inp, Index) -> |
| 210 |
case re:run(Inp, RE, [anchored]) of |
| 211 |
{match, [{0, Length}|_]} -> |
| 212 |
{Head, Tail} = erlang:split_binary(Inp, Length), |
| 213 |
{Head, Tail, p_advance_index(Head, Index)}; |
| 214 |
_ -> {fail, {expected, {character_class, binary_to_list(Class)}, Index}} |
| 215 |
end |
| 216 |
end. |
| 217 |
-endif. |
| 218 |
|
| 219 |
-ifdef(p_regexp). |
| 220 |
-spec p_regexp(binary()) -> parse_fun(). |
| 221 |
p_regexp(Regexp) -> |
| 222 |
{ok, RE} = re:compile(Regexp, [unicode, dotall, anchored]), |
| 223 |
fun(Inp, Index) -> |
| 224 |
case re:run(Inp, RE) of |
| 225 |
{match, [{0, Length}|_]} -> |
| 226 |
{Head, Tail} = erlang:split_binary(Inp, Length), |
| 227 |
{Head, Tail, p_advance_index(Head, Index)}; |
| 228 |
_ -> {fail, {expected, {regexp, binary_to_list(Regexp)}, Index}} |
| 229 |
end |
| 230 |
end. |
| 231 |
-endif. |
| 232 |
|
| 233 |
-ifdef(line). |
| 234 |
-spec line(index() | term()) -> pos_integer() | undefined. |
| 235 |
line({{line,L},_}) -> L; |
| 236 |
line(_) -> undefined. |
| 237 |
-endif. |
| 238 |
|
| 239 |
-ifdef(column). |
| 240 |
-spec column(index() | term()) -> pos_integer() | undefined. |
| 241 |
column({_,{column,C}}) -> C; |
| 242 |
column(_) -> undefined. |
| 243 |
-endif. |
| 244 |
|
| 245 |
-spec p_advance_index(input() | unicode:charlist() | pos_integer(), index()) -> index(). |
| 246 |
p_advance_index(MatchedInput, Index) when is_list(MatchedInput) orelse is_binary(MatchedInput)-> % strings |
| 247 |
lists:foldl(fun p_advance_index/2, Index, unicode:characters_to_list(MatchedInput)); |
| 248 |
p_advance_index(MatchedInput, Index) when is_integer(MatchedInput) -> % single characters |
| 249 |
{{line, Line}, {column, Col}} = Index, |
| 250 |
case MatchedInput of |
| 251 |
$\n -> {{line, Line+1}, {column, 1}}; |
| 252 |
_ -> {{line, Line}, {column, Col+1}} |
| 253 |
end. |