aboutsummaryrefslogtreecommitdiffstats
path: root/lib/syntax_tools/examples
diff options
context:
space:
mode:
authorErlang/OTP <[email protected]>2009-11-20 14:54:40 +0000
committerErlang/OTP <[email protected]>2009-11-20 14:54:40 +0000
commit84adefa331c4159d432d22840663c38f155cd4c1 (patch)
treebff9a9c66adda4df2106dfd0e5c053ab182a12bd /lib/syntax_tools/examples
downloadotp-84adefa331c4159d432d22840663c38f155cd4c1.tar.gz
otp-84adefa331c4159d432d22840663c38f155cd4c1.tar.bz2
otp-84adefa331c4159d432d22840663c38f155cd4c1.zip
The R13B03 release.OTP_R13B03
Diffstat (limited to 'lib/syntax_tools/examples')
-rw-r--r--lib/syntax_tools/examples/Makefile58
-rw-r--r--lib/syntax_tools/examples/demo.erl80
-rw-r--r--lib/syntax_tools/examples/test.erl25
-rw-r--r--lib/syntax_tools/examples/test_comprehensions.erl39
4 files changed, 202 insertions, 0 deletions
diff --git a/lib/syntax_tools/examples/Makefile b/lib/syntax_tools/examples/Makefile
new file mode 100644
index 0000000000..a52a52a50c
--- /dev/null
+++ b/lib/syntax_tools/examples/Makefile
@@ -0,0 +1,58 @@
+# ``The contents of this file are subject to the Erlang Public License,
+# Version 1.1, (the "License"); you may not use this file except in
+# compliance with the License. You should have received a copy of the
+# Erlang Public License along with this software. If not, it can be
+# retrieved via the world wide web at http://www.erlang.org/.
+#
+# Software distributed under the License is distributed on an "AS IS"
+# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+# the License for the specific language governing rights and limitations
+# under the License.
+#
+# The Initial Developer of the Original Code is Ericsson Utvecklings AB.
+# Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
+# AB. All Rights Reserved.''
+#
+# $Id$
+#
+include $(ERL_TOP)/make/target.mk
+include $(ERL_TOP)/make/$(TARGET)/otp.mk
+
+# ----------------------------------------------------
+# Application version
+# ----------------------------------------------------
+include ../vsn.mk
+VSN=$(SYNTAX_TOOLS_VSN)
+
+# ----------------------------------------------------
+# Release Macros
+# ----------------------------------------------------
+RELSYSDIR = $(RELEASE_PATH)/lib/syntax_tools-$(VSN)
+
+# ----------------------------------------------------
+# Macros
+# ----------------------------------------------------
+
+EXAMPLE_FILES = demo.erl
+
+# ----------------------------------------------------
+# Make Rules
+# ----------------------------------------------------
+debug opt:
+
+clean:
+
+docs:
+
+
+# ----------------------------------------------------
+# Release Targets
+# ----------------------------------------------------
+include $(ERL_TOP)/make/otp_release_targets.mk
+
+release_spec:
+ $(INSTALL_DIR) $(RELSYSDIR)/examples
+ $(INSTALL_DATA) $(EXAMPLE_FILES) $(RELSYSDIR)/examples
+
+release_docs_spec:
+
diff --git a/lib/syntax_tools/examples/demo.erl b/lib/syntax_tools/examples/demo.erl
new file mode 100644
index 0000000000..53ba1372fd
--- /dev/null
+++ b/lib/syntax_tools/examples/demo.erl
@@ -0,0 +1,80 @@
+%%
+%% Demo file for the Syntax Tools package.
+%%
+%% The program is self-instructing. Compile `demo' from the shell and
+%% execute `demo:run()'.
+
+-module(demo).
+
+-export([run/0, run_1/0, run_2/0, run_3/0, run_4/0,
+ view/1, view/2, view/3]).
+
+small_file() -> "test.erl".
+
+medium_file() -> "test_comprehensions.erl".
+
+big_file() -> "erl_comment_scan.erl".
+
+run() ->
+ make:all([load]),
+ io:fwrite("\n\n** Enter `demo:run_1()' to parse and pretty-print\n"
+ "the file \"~s\" with the default field width.\n\n",
+ [small_file()]),
+ ok.
+
+run_1() ->
+ view(small_file()),
+ io:fwrite("\n\n\n** Enter `demo:run_2()' to parse and pretty-print\n"
+ "the file \"~s\" with a small field width.\n\n",
+ [small_file()]),
+ ok.
+
+run_2() ->
+ view(small_file(), 15),
+ io:fwrite("\n\n\n** Enter `demo:run_3()' to parse and pretty-print\n"
+ "the file \"~s\" with field width 35\n\n",
+ [medium_file()]),
+ ok.
+
+run_3() ->
+ view(medium_file(), 35),
+ io:fwrite("\n\n\n** Enter `demo:run_4()' to parse and pretty-print\n"
+ "the file \"~s\" with field width 55 and ribbon width 40.\n\n",
+ [big_file()]),
+ ok.
+
+run_4() ->
+ view(big_file(), 55, 40),
+ io:fwrite("\n\n\n** Done! Now you can play around with the function\n"
+ "`demo:view(FileName, PaperWidth, RibbonWidth)' on any\n"
+ "Erlang source files you have around you.\n"
+ "(Include the \".erl\" suffix in the file name.\n"
+ "RibbonWidth and PaperWidth are optional.)\n\n"),
+ ok.
+
+view(Name) ->
+ SyntaxTree = read(Name),
+ print(SyntaxTree).
+
+view(Name, Paper) ->
+ SyntaxTree = read(Name),
+ print(SyntaxTree, Paper).
+
+view(Name, Paper, Ribbon) ->
+ SyntaxTree = read(Name),
+ print(SyntaxTree, Paper, Ribbon).
+
+print(SyntaxTree) ->
+ io:put_chars(erl_prettypr:format(SyntaxTree)).
+
+print(SyntaxTree, Paper) ->
+ io:put_chars(erl_prettypr:format(SyntaxTree, [{paper, Paper}])).
+
+print(SyntaxTree, Paper, Ribbon) ->
+ io:put_chars(erl_prettypr:format(SyntaxTree, [{paper, Paper},
+ {ribbon, Ribbon}])).
+
+read(Name) ->
+ {ok, Forms} = epp:parse_file(Name, [], []),
+ Comments = erl_comment_scan:file(Name),
+ erl_recomment:recomment_forms(Forms, Comments).
diff --git a/lib/syntax_tools/examples/test.erl b/lib/syntax_tools/examples/test.erl
new file mode 100644
index 0000000000..087c49ed4c
--- /dev/null
+++ b/lib/syntax_tools/examples/test.erl
@@ -0,0 +1,25 @@
+%%
+%% This is a test file
+%%
+
+-module(test).
+
+-export([nrev/1]).
+
+%% Just a naive reverse function in order
+%% to get a code example with some comments.
+
+nrev([X | Xs]) ->
+ append(X, nrev(Xs)); % Quadratic behaviour
+nrev([]) ->
+ %% The trivial case:
+ [].
+
+ %% We need `append' as a subroutine:
+
+append(Y, [X | Xs]) ->
+ [X | append(Y, Xs)]; % Simple, innit?
+append(Y, []) ->
+ [Y]. % Done.
+
+%% ---- end of file ----
diff --git a/lib/syntax_tools/examples/test_comprehensions.erl b/lib/syntax_tools/examples/test_comprehensions.erl
new file mode 100644
index 0000000000..ede9caeaed
--- /dev/null
+++ b/lib/syntax_tools/examples/test_comprehensions.erl
@@ -0,0 +1,39 @@
+%%%-------------------------------------------------------------------
+%%% File : test_comprehensions.erl
+%%% Author : Per Gustafsson <pergu@jobberl>
+%%% Description : Test module to see that pretty printing etc.
+%%% works on extended comprehensions
+%%% Created : 15 Oct 2007 by Per Gustafsson <pergu@jobberl>
+%%%-------------------------------------------------------------------
+-module(test_comprehensions).
+
+-compile(binary_comprehension).
+
+-export([test/0]).
+
+test() ->
+ {bbc(),llc(),blc(),lbc(),bblc(),lblc()}.
+
+binary() ->
+ <<1,2,3>>.
+
+list() ->
+ [1,2,3].
+
+bbc() ->
+ << <<X>> || <<X>> <= binary(), X > 1 >>.
+
+llc() ->
+ [X || X <- list(), X > 1].
+
+blc() ->
+ << <<X>> || X <- list(), X > 1 >>.
+
+lbc() ->
+ [X || <<X>> <= binary(), X > 1].
+
+bblc() ->
+ << <<(X+Y)>> || <<X>> <= binary(), Y <- list(), X > 1 >>.
+
+lblc() ->
+ [(X+Y) || <<X>> <= binary(), Y <- list(), X > 1].