Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.\#*
\#*
*~
*.beam
ebin
/_build/
/rebar.lock
41 changes: 35 additions & 6 deletions Makefile
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
SUBDIRS=src \
samples/sip
# samples/sdp \
.PHONY: clean distclean upgrade compile test dialyzer eunit xref
.PHONY: release

include ./subdir.mk
include ./vsn.mk
default: compile

APPNAME=abnf
clean:
./rebar3 clean
rm -rf _build/*/rel
rm -f _build/*/lib/*/ebin/*
find . -name "erlcinfo" -exec rm {} \;

distclean: clean
rm -rf _build
rm -f rebar.lock
rm -rf .release

upgrade:
./rebar3 upgrade

compile:
./rebar3 compile

test: xref eunit dialyzer

dialyzer:
./rebar3 dialyzer

eunit:
./rebar3 eunit

xref:
./rebar3 xref

release: clean
./rebar3 xref
./rebar3 eunit
./rebar3 release
Empty file removed ebin/.gitignore
Empty file.
58 changes: 0 additions & 58 deletions include.mk

This file was deleted.

5 changes: 5 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
%-*- mode: erlang; erlang-indent-level: 2 -*-
{erl_opts, [debug_info, warnings_as_errors]}.
{xref_checks, [undefined_function_calls]}.
{cover_enabled, true}.
{cover_print_enabled, true}.
14 changes: 0 additions & 14 deletions src/Makefile

This file was deleted.

5 changes: 2 additions & 3 deletions src/abnfc.app.src
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
%% -*- erlang -*-
{application, abnfc,
[{description,"ABNF parser generator"},
{vsn,"0.3"},
{modules, "MODULES"},
{vsn,"0.3.1"},
{modules, []},
{applications,[kernel,stdlib]},
{registered,[]},
{env,[]}
]}.
111 changes: 38 additions & 73 deletions src/abnfc.erl
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
%%%-------------------------------------------------------------------
%%% @copyright 2009 Anders Nygren
%%% @version {@vsn}
%%% @version {@vsn}
%%% @author Anders Nygren <anders.nygren@gmail.com>
%%% @doc
%%% @end
%%% @doc
%%% @end
%%%-------------------------------------------------------------------
-module(abnfc).

%% API
-export([file/1, file/2,
parse/1, parse/2]).
parse/1, parse/2]).

-export([erlangcode/0]).

-compile(export_all).
%%====================================================================
%% API
%%====================================================================
Expand All @@ -34,27 +33,18 @@ file(File) ->
%% @end
%%--------------------------------------------------------------------
file(File, Opts) when is_list(Opts) ->
case read_file(File) of
{ok, Name, Text} ->
POpts = [],
GenOpts = gen_opts(Name, Opts),
COpts = compiler_opts(Opts),
case parse(Text, POpts) of
{ok, AST, _Rest} ->
AST1 = abnfc_ast:ast_to_int_form(AST),
case proplists:get_bool(verbose,Opts) of
true -> io:format("~p~n",[AST1]);
false -> ok
end,
{ok, Code} = abnfc_gen:generate(AST1, GenOpts),
{ok, GenFile} = write_file(Code, GenOpts ++ Opts),
compile_file(GenFile, COpts, Opts);
Error ->
Error
end;
Error ->
Error
end.
{ok, Name, Text} = read_file(File),
GenOpts = gen_opts(Name, Opts),
COpts = compiler_opts(Opts),
{ok, AST, []} = parse(Text, []),
AST1 = abnfc_ast:ast_to_int_form(AST),
case proplists:get_bool(verbose,Opts) of
true -> io:format("~p~n",[AST1]);
false -> ok
end,
{ok, Code} = abnfc_gen:generate(AST1, GenOpts),
{ok, GenFile} = write_file(Code, GenOpts ++ Opts),
compile_file(GenFile, COpts, Opts).

%%--------------------------------------------------------------------
%% @spec (Text) -> {ok, AST, Rest::binary()} | fail
Expand Down Expand Up @@ -84,76 +74,51 @@ parse(String, _Opts) when is_list(String) ->
%%--------------------------------------------------------------------
erlangcode() ->
fun (T) ->
scan(T)
scan(T)
end.

scan(Input) ->
case erl_scan:tokens([], Input, 1) of
{done, {ok, Toks, _EndLine}, Extra} ->
{ok,Abs} = erl_parse:parse_exprs(Toks),
{ok, Abs, Extra};
{more, _Cont} ->
throw(end_of_input)
{done, {ok, Toks, _EndLine}, Extra} ->
{ok,Abs} = erl_parse:parse_exprs(Toks),
{ok, Abs, Extra};
{more, _Cont} ->
throw(end_of_input)
end.

%%--------------------------------------------------------------------
%% @private
%% @spec (Tokens) -> list()
%% @doc Convert tokens returned by erl_scan to a string again.
%% @end
%%--------------------------------------------------------------------
toks_to_list(Tokens) ->
lists:foldl(fun({atom,L,Name},{Line, Acc}) ->
{L,["'",Name,"'",sep(L,Line)|Acc]};
({string,L,Name},{Line, Acc}) ->
{L,["\"",Name,"\"",sep(L,Line)|Acc]};
({_Type,L,Name},{Line, Acc}) ->
{L,[Name,sep(L,Line)|Acc]};
({dot,_L},{_Line,Acc}) ->
lists:concat(lists:reverse(Acc));
({Reserved, L},{Line,Acc}) ->
{L,[Reserved,sep(L,Line)|Acc]}
end, {1,[]}, Tokens).

sep(L,L) ->
" ";
sep(_,_) ->
"\n".

%%====================================================================
%% Internal functions
%%====================================================================
read_file(File) ->
case string:tokens(filename:basename(File), ".") of
[Name,"set","abnf"] ->
{ok, Files} = file:consult(File),
{ok, Name, lists:flatten([read_file1(F) || F <- Files])};
[Name, "abnf"] ->
{ok, Name, read_file1(File)}
[Name,"set","abnf"] ->
{ok, Files} = file:consult(File),
{ok, Name, lists:flatten([read_file1(F) || F <- Files])};
[Name, "abnf"] ->
{ok, Name, read_file1(File)}
end.

read_file1(File) ->
{ok, Bin} = file:read_file(File),
binary_to_list(Bin).

gen_opts(Name, Opts) ->
Mod = proplists:get_value(mod, Opts, Name),
Type = case proplists:get_bool(binary, Opts) of
true -> binary;
false -> list
end,
true -> binary;
false -> list
end,
Verbose = proplists:get_bool(verbose,Opts),
[{mod,Mod},{verbose,Verbose},Type].

compiler_opts(Opts) ->
OutDir = proplists:get_value(o, Opts, "./"),
IncludeDirs = [{i,Dir}||Dir <- proplists:get_all_values(i, Opts)],
[report,{outdir,OutDir}|IncludeDirs].

write_file(Code, Opts) ->
Name = filename:join(proplists:get_value(o, Opts, "."),
proplists:get_value(mod, Opts))++".erl",
proplists:get_value(mod, Opts))++".erl",

maybe_write("abnfc: writing to ~p~n",[Name],Opts),
file:write_file(Name, Code),
erl_tidy:file(Name,[{paper, 95},{backups,false}]),
Expand All @@ -162,12 +127,12 @@ write_file(Code, Opts) ->
compile_file(File, COpts, MyOpts) ->
maybe_write("abnfc: compiling ~p opts = ~p~n",[File, COpts],MyOpts),
case proplists:get_bool(noobj,MyOpts) of
true -> ok;
false -> compile:file(File, COpts)
true -> ok;
false -> compile:file(File, COpts)
end.

maybe_write(Fmt,Args,Opts) ->
case proplists:get_bool(verbose,Opts) of
true -> io:format(Fmt,Args);
false -> ok
true -> io:format(Fmt,Args);
false -> ok
end.
25 changes: 13 additions & 12 deletions src/abnfc_ast.erl
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
%%%-------------------------------------------------------------------
%%% @copyright 2009 Anders Nygren
%%% @version {@vsn}
%%% @version {@vsn}
%%% @author Anders Nygren <anders.nygren@gmail.com>
%%% @doc
%%% @end
%%% @doc
%%% @end
%%%-------------------------------------------------------------------
-module(abnfc_ast).

%% API
%%-export([transform/2]).
-compile(export_all).
-export(
[ast_to_int_form/1,
int_transform/2
]).

-include("abnfc_ast.hrl").
-include("../include/abnfc_ast.hrl").

%%====================================================================
%% API
Expand All @@ -24,7 +26,6 @@
ast_to_int_form(#rulelist{rules=Rules}) ->
[R#rule{body=transform(R#rule.body)}||R <- Rules].


int_transform(INT, Fun) ->
[R#rule{body=Fun(R#rule.body)} || R <- INT].

Expand All @@ -37,10 +38,10 @@ transform(#rule{body=Body}=R) ->
transform(#alt{alts=Alts}=A) ->
Alts1 = [transform(Alt)||Alt <- Alts],
case char_alts(Alts1) of
[C] when is_record(C,char_alt) ->
C;
Alts2 ->
A#alt{alts=Alts2}
[C] when is_record(C,char_alt) ->
C;
Alts2 ->
A#alt{alts=Alts2}
end;
transform(#seq{elements=Elems}=S) ->
Elems1 = [transform(Elem)||Elem <- Elems],
Expand All @@ -54,7 +55,7 @@ char_alts(Alts) ->
char_alts(Alts, {[],[]}).

char_alts([Alt|Alts], {Chars,Others}) when is_record(Alt, char_val);
is_record(Alt, char_range) ->
is_record(Alt, char_range) ->
char_alts(Alts,{Chars++[Alt], Others});
char_alts([Alt|Alts], {Chars,Others}) ->
char_alts(Alts,{Chars, Others++[Alt]});
Expand Down
Loading