[m-dev.] for review: improve higher-order syntax
Fergus Henderson
fjh at cs.mu.OZ.AU
Fri Feb 11 17:55:30 AEDT 2000
This is in response to Ralph Becket's mail to
mercury-users...
----------
Estimated hours taken: 1.5
Generalize the higher-order term syntax:
in `Foo(Args)', allow Foo to be any term,
not just a variable.
doc/reference_manual.texi:
Document the new higher-order term syntax.
library/parser.m:
Implement the new higher-order term syntax.
Workspace: /d-drive/home/hg/fjh/mercury
Index: doc/reference_manual.texi
===================================================================
RCS file: /home/mercury1/repository/mercury/doc/reference_manual.texi,v
retrieving revision 1.172
diff -u -d -r1.172 reference_manual.texi
--- doc/reference_manual.texi 2000/02/11 05:04:05 1.172
+++ doc/reference_manual.texi 2000/02/11 05:44:13
@@ -317,27 +317,34 @@
A functor is an integer, a float, a string, a name, a compound term,
or a higher-order term.
-A compound term is a name followed without any intervening
+A compound term is a simple compound term, an operator term,
+or a parenthesized term.
+
+A simple compound term is a name followed without any intervening
whitespace by an open parenthesis (i.e. an open_ct token),
a sequence of argument terms separated by commas, and a close
-parenthesis. Compound terms may also be specified using
-operator notation, as in Prolog.
+parenthesis.
-Operators can also be formed by enclosing an variable or name between grave
+An operator term is a term specified using operator notation, as in Prolog.
+Operators can also be formed by enclosing a variable or name between grave
accents (backquotes). Any variable or name may
be used as an operator in this way. If @var{fun} is a variable or name,
then a term of the form @code{@var{X} `@var{fun}` @var{Y}} is equivalent to
@code{@var{fun}(@var{X}, @var{Y})}. The operator is treated as having the
highest precedence possible and is left associative.
-A higher-order term is a variable followed without any intervening
-whitespace by an open parenthesis (i.e. an open_ct token),
+A parenthesized term is just an open parenthesis
+followed by a term and a close parenthesis.
+
+A higher-order term is a term, other than a name or an operator term,
+followed without
+any intervening whitespace by an open parenthesis (i.e. an open_ct token),
a sequence of argument terms separated by commas, and a close
-parenthesis. A higher-order term is equivalent to a compound term
+parenthesis. A higher-order term is equivalent to a simple compound term
whose functor is the empty name, and whose arguments are the
the variable followed by the arguments of the higher-order term.
That is, a term such as @code{Var(Arg1, @dots{}, ArgN)} is
-parsed as @code{''(Var, Arg1, @dots{}, ArgN)},
+parsed as @code{''(Var, Arg1, @dots{}, ArgN)}.
@node Items
@section Items
Index: library/parser.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/parser.m,v
retrieving revision 1.33
diff -u -d -r1.33 parser.m
--- library/parser.m 1999/07/12 07:44:37 1.33
+++ library/parser.m 2000/02/11 06:15:56
@@ -421,7 +421,7 @@
parser__parse_simple_term(Token, Context, Priority, Term) -->
( parser__parse_simple_term_2(Token, Context, Priority, Term0) ->
- { Term = Term0 }
+ parser__check_for_higher_order_term(Term0, Context, Term)
;
parser__unexpected_tok(Token, Context,
"unexpected token at start of (sub)term", Term)
@@ -472,29 +472,9 @@
{ Term = ok(term__functor(term__atom(Atom), [], TermContext)) }
).
-parser__parse_simple_term_2(variable(VarName), Context, _, Term) -->
+parser__parse_simple_term_2(variable(VarName), _, _, Term) -->
parser__add_var(VarName, Var),
- %
- % As an extension to ISO Prolog syntax,
- % we check for the syntax "Var(Args)", and parse it
- % as the term ''(Var, Args). The aim of this extension
- % is to provide a nicer syntax for higher-order stuff.
- %
- ( parser__get_token(open_ct) ->
- parser__get_term_context(Context, TermContext),
- parser__parse_args(Args0),
- ( { Args0 = ok(Args) },
- { Term = ok(term__functor(term__atom(""),
- [term__variable(Var) | Args],
- TermContext)) }
- ;
- % propagate error upwards
- { Args0 = error(Message, Tokens) },
- { Term = error(Message, Tokens) }
- )
- ;
- { Term = ok(term__variable(Var)) }
- ).
+ { Term = ok(term__variable(Var)) }.
parser__parse_simple_term_2(integer(Int), Context, _, Term) -->
parser__get_term_context(Context, TermContext),
@@ -550,6 +530,34 @@
% propagate error upwards
{ Term = SubTerm0 }
)
+ ).
+
+:- pred parser__check_for_higher_order_term(parse(term(T)), token_context,
+ parse(term(T)), parser__state(T), parser__state(T)).
+:- mode parser__check_for_higher_order_term(in, in, out, in, out) is det.
+
+parser__check_for_higher_order_term(Term0, Context, Term) -->
+ %
+ % As an extension to ISO Prolog syntax,
+ % we check for the syntax "Term(Args)", and parse it
+ % as the term ''(Term, Args). The aim of this extension
+ % is to provide a nicer syntax for higher-order stuff.
+ %
+ ( { Term0 = ok(Term1) }, parser__get_token(open_ct) ->
+ parser__get_term_context(Context, TermContext),
+ parser__parse_args(Args0),
+ ( { Args0 = ok(Args) },
+ { Term2 = ok(term__functor(term__atom(""),
+ [Term1 | Args],
+ TermContext)) },
+ parser__check_for_higher_order_term(Term2, Context, Term)
+ ;
+ % propagate error upwards
+ { Args0 = error(Message, Tokens) },
+ { Term = error(Message, Tokens) }
+ )
+ ;
+ { Term = Term0 }
).
:- pred parser__parse_special_atom(string, term__context, parse(term(T)),
--
Fergus Henderson <fjh at cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3 | -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to: mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions: mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------
More information about the developers
mailing list