From 008dcb8542ed4bb1aed98e3fed92c55b0770d132 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Wed, 15 Dec 2010 15:08:15 +0100 Subject: Add abnfc support abnfc is an ABNF parser generator. Options are: - doc_root (defaults to "src") - out_dir (defaults to "src") - source_ext (defaults to ".abnf") - module_ext (defaults to "") --- src/rebar_abnfc_compiler.erl | 109 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/rebar_abnfc_compiler.erl (limited to 'src/rebar_abnfc_compiler.erl') diff --git a/src/rebar_abnfc_compiler.erl b/src/rebar_abnfc_compiler.erl new file mode 100644 index 0000000..743b3fe --- /dev/null +++ b/src/rebar_abnfc_compiler.erl @@ -0,0 +1,109 @@ +%% -*- tab-width: 4;erlang-indent-level: 4;indent-tabs-mode: nil -*- +%% ex: ts=4 sw=4 et +%% ------------------------------------------------------------------- +%% +%% rebar: Erlang Build Tools +%% +%% Copyright (c) 2010 Anthony Ramine (nox@dev-extend.eu), +%% +%% Permission is hereby granted, free of charge, to any person obtaining a copy +%% of this software and associated documentation files (the "Software"), to deal +%% in the Software without restriction, including without limitation the rights +%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +%% copies of the Software, and to permit persons to whom the Software is +%% furnished to do so, subject to the following conditions: +%% +%% The above copyright notice and this permission notice shall be included in +%% all copies or substantial portions of the Software. +%% +%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +%% THE SOFTWARE. +%% ------------------------------------------------------------------- + +%% The rebar_abnfc_compiler module is a plugin for rebar that compiles +%% ABNF grammars into parsers. By default, it compiles all src/*.abnf +%% to src/*.erl. +%% +%% Configuration options should be placed in rebar.config under +%% 'abnfc_opts'. Available options include: +%% +%% doc_root: where to find the ABNF grammars to compile +%% "src" by default +%% +%% out_dir: where to put the generated files. +%% "src" by default +%% +%% source_ext: the file extension the ABNF grammars have. +%% ".abnf" by default +%% +%% module_ext: characters to append to the parser's module name +%% "" by default +-module(rebar_abnfc_compiler). + +-export([compile/2]). + +-include("rebar.hrl"). + +%% =================================================================== +%% Public API +%% =================================================================== + +compile(Config, _AppFile) -> + DtlOpts = abnfc_opts(Config), + rebar_base_compiler:run(Config, [], + option(doc_root, DtlOpts), + option(source_ext, DtlOpts), + option(out_dir, DtlOpts), + option(module_ext, DtlOpts) ++ ".erl", + fun compile_abnfc/3). + + +%% =================================================================== +%% Internal functions +%% =================================================================== + +abnfc_opts(Config) -> + rebar_config:get(Config, abnfc_opts, []). + +option(Opt, DtlOpts) -> + proplists:get_value(Opt, DtlOpts, default(Opt)). + +default(doc_root) -> "src"; +default(out_dir) -> "src"; +default(source_ext) -> ".abnf"; +default(module_ext) -> "". + +abnfc_is_present() -> + code:which(abnfc) =/= non_existing. + +compile_abnfc(Source, _Target, Config) -> + case abnfc_is_present() of + false -> + ?CONSOLE( + "~n===============================================~n" + " You need to install abnfc to compile ABNF grammars~n" + " Download the latest tarball release from github~n" + " https://github.com/nygge/abnfc~n" + " and install it into your erlang library dir~n" + "===============================================~n~n", []), + ?FAIL; + true -> + AbnfcOpts = abnfc_opts(Config), + SourceExt = option(source_ext, AbnfcOpts), + Opts = [noobj, + {o, option(out_dir, AbnfcOpts)}, + {mod, filename:basename(Source, SourceExt) ++ + option(module_ext, AbnfcOpts)}], + case abnfc:file(Source, Opts) of + ok -> ok; + Error -> + ?CONSOLE("Compiling grammar ~s failed:~n ~p~n", + [Source, Error]), + ?FAIL + end + end. -- cgit v1.1 From c466076ffb5a1ea4c00d49fefff0dcfbceb58236 Mon Sep 17 00:00:00 2001 From: Tuncer Ayaz Date: Mon, 31 Jan 2011 17:43:31 +0100 Subject: Clean up emacs file local variables --- src/rebar_abnfc_compiler.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/rebar_abnfc_compiler.erl') diff --git a/src/rebar_abnfc_compiler.erl b/src/rebar_abnfc_compiler.erl index 743b3fe..80fdf5e 100644 --- a/src/rebar_abnfc_compiler.erl +++ b/src/rebar_abnfc_compiler.erl @@ -1,4 +1,4 @@ -%% -*- tab-width: 4;erlang-indent-level: 4;indent-tabs-mode: nil -*- +%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*- %% ex: ts=4 sw=4 et %% ------------------------------------------------------------------- %% -- cgit v1.1 From 63de05d914f3c2bef6dcfc6cf966400d93c9c80d Mon Sep 17 00:00:00 2001 From: Tuncer Ayaz Date: Fri, 28 Jan 2011 16:08:27 +0100 Subject: Clean up code --- src/rebar_abnfc_compiler.erl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/rebar_abnfc_compiler.erl') diff --git a/src/rebar_abnfc_compiler.erl b/src/rebar_abnfc_compiler.erl index 80fdf5e..bfb9a35 100644 --- a/src/rebar_abnfc_compiler.erl +++ b/src/rebar_abnfc_compiler.erl @@ -85,12 +85,12 @@ compile_abnfc(Source, _Target, Config) -> case abnfc_is_present() of false -> ?CONSOLE( - "~n===============================================~n" - " You need to install abnfc to compile ABNF grammars~n" - " Download the latest tarball release from github~n" - " https://github.com/nygge/abnfc~n" - " and install it into your erlang library dir~n" - "===============================================~n~n", []), + <<"~n===============================================~n" + " You need to install abnfc to compile ABNF grammars~n" + " Download the latest tarball release from github~n" + " https://github.com/nygge/abnfc~n" + " and install it into your erlang library dir~n" + "===============================================~n~n">>, []), ?FAIL; true -> AbnfcOpts = abnfc_opts(Config), @@ -98,7 +98,7 @@ compile_abnfc(Source, _Target, Config) -> Opts = [noobj, {o, option(out_dir, AbnfcOpts)}, {mod, filename:basename(Source, SourceExt) ++ - option(module_ext, AbnfcOpts)}], + option(module_ext, AbnfcOpts)}], case abnfc:file(Source, Opts) of ok -> ok; Error -> -- cgit v1.1