Skip to content

Commit 701b462

Browse files
author
Guenter Kniesel
committed
NEW: Files for testing Prolog parser / demonstrating legal and illegal Prolog syntax.
1 parent ae4be99 commit 701b462

File tree

4 files changed

+167
-0
lines changed

4 files changed

+167
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
/*
3+
Testcases of all syntax variations.
4+
Everything that does not require cross-file scoping.
5+
*/
6+
7+
%============== Facts with all kinds of terms as parameters ==========
8+
9+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10+
% Integers and decimal numbers:
11+
a(0). % integer
12+
a(1.0). % decimal number
13+
a(1,0). % two arguments!
14+
15+
% 2.15.1.5 Using digit groups in large integers
16+
%
17+
% SWI-Prolog supports splitting long integers into digit groups.
18+
% Digit groups can be separated with the sequence <underscore>,
19+
% <optional white space>. If the <radix> is 10 or lower, they
20+
% may also be separated with exactly one space.
21+
% http://www.swi-prolog.org/pldoc/man?section=digitgroupsyntax
22+
% The following all express the integer 1 million:
23+
24+
a(1_000_000).
25+
a(1 000 000).
26+
a(1_000_/*more*/000).
27+
28+
29+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
30+
% 2.15.1.4 Syntax for non-decimal numbers
31+
% Edinburgh syntax:
32+
% <radix>'<number>, where <radix> is a number between 2 and 36.
33+
% ISO syntax:
34+
% 0[bxo]<number>. For example: A is 0b100 \/ 0xf00 is a valid expression.
35+
% http://www.swi-prolog.org/pldoc/man?section=nondecsyntax
36+
37+
a(2'1010). % Edinburgh: binary representation of 10
38+
a(8'12). % Edinburgh: octal representation of 10
39+
a(16'A). % Edinburgh: hexadecimal representation of 10
40+
41+
%a(1'1). % Edinburgh: Illegal, radix must be above 1
42+
% <-- Syntax error: Operator expected (is not the most helpful
43+
% <-- message but at least it is an error message
44+
%a(37'1). % Edinburgh: Illegal, radix must be below 37
45+
% <-- Throws no error at this point but many lines below it will
46+
% <-- say: Syntax error: String too long (see style_check/1)
47+
48+
a(0b1010). % ISO: binary representation of 10
49+
a(0o12). % ISO: octal representation of 10
50+
a(0xA). % ISO: hexadecimal representation of 10
51+
52+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53+
% 2.15.1.2 Nested comments
54+
/*
55+
SWI-Prolog allows for nesting /* ... */ comments like this one
56+
*/
57+
% http://www.swi-prolog.org/pldoc/man?section=nestedcomments
58+
59+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60+
% 2.15.1.3 Character Escape Syntax
61+
% Within quoted atoms special characters are represented using escape
62+
% sequences. An escape sequence is led in by the backslash (\) character.
63+
% Character escaping is only available if
64+
% current_prolog_flag(character_escapes, true) is active (default).
65+
% http://www.swi-prolog.org/pldoc/man?section=charescapes
66+
67+
a('abc-\a\b\c\
68+
\e\f\n\r\s\t\v-def').
69+
% The code \xa\3 emits the character 10 (hexadecimal `a') followed by `3'.
70+
% Characters specified this way are interpreted as Unicode characters. See also \u.
71+
a('abc-\xa\3-def').
72+
a('abc-\u1111-def').
73+
%a('abc-\U11111111-def'). % Legal unicode sign in the Hangul character set
74+
%a('\U11111111'). % Legal unicode sign in the Hangul character set
75+
a('\40').
76+
a('\\').
77+
a('\"').
78+
a('\'').
79+
a('\`').
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
a(f).
3+
a(f()). % Illegal before SWI-Prolog 7.x
4+
a(f(1)).
5+
a(f(_)).
6+
a(f(X,X,Y,Y,1,2,3,4)).
7+
a('blub'(1)).
8+
a(_blub(1)). % Illegal
9+
10+
a([]).
11+
a([1,2,3]).
12+
a([1|[2,3]]).
13+
a([1|2,3]). % Illegal
14+
15+

PDT Examples/CTeditorTests/quotes.pl

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
% Demonstrate the effect of \c:
2+
3+
:- module( quotes_demo,
4+
[ a/1
5+
] ).
6+
7+
8+
a(atom__(X)) :-
9+
X = 'alpha
10+
bravo
11+
charlie'.
12+
13+
a(atom_c(X)) :-
14+
X = 'alpha
15+
bravo \c
16+
charlie'.
17+
18+
a(string__(X)) :-
19+
X = "alpha
20+
bravo
21+
charlie".
22+
23+
a(string_c(X)) :-
24+
X = "alpha
25+
bravo \c
26+
charlie".
27+
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
2.15.1.9 Singleton variable checking
3+
4+
A singleton variable is a variable that appears only one time in a
5+
clause. It can always be replaced by _, the anonymous variable.
6+
7+
In some cases, however, people prefer to give the variable a name.
8+
As mistyping a variable is a common mistake, Prolog systems generally
9+
give a warning (controlled by style_check/1) if a variable is used
10+
only once.
11+
The system can be informed that a variable is meant to appear once by
12+
starting it with an underscore, e.g., _Name.
13+
Please note that any variable, except plain _, shares with variables
14+
of the same name. The term t(_X, _X) is equivalent to t(X, X), which
15+
is different from t(_, _).
16+
% http://www.swi-prolog.org/pldoc/man?section=singleton
17+
*/
18+
test(_).
19+
test(_a). % Singleton variables: [_a]
20+
test(_12). % Singleton variables: [_12]
21+
test(A). % Singleton variables: [A]
22+
test(_A).
23+
test(__a).
24+
test(_, _).
25+
test(_a, _a).
26+
test(__a, __a). % Singleton-marked variables appearing more than once: [__a]
27+
test(_A, _A). % Singleton-marked variables appearing more than once: [_A]
28+
test(A, A).
29+
30+
/*
31+
Semantic singletons
32+
33+
Starting with version 6.5.1, SWI-Prolog has syntactic singletons and
34+
semantic singletons. The first are checked by read_clause/3 (and
35+
read_term/3 using the option singletons(warning)).
36+
37+
The latter are generated by the compiler for variables that appear
38+
alone in a branch. For example, in the code below the variable X is
39+
not a syntactic singleton, but the variable X does not communicate any
40+
bindings and replacing X with _ does not change the semantics.
41+
*/
42+
43+
test :-
44+
( test_1(X)
45+
; test_2(X)
46+
).

0 commit comments

Comments
 (0)