-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportMachine.pl
31 lines (27 loc) · 921 Bytes
/
portMachine.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
:- module(portMachine, [portMachine/3]).
:- use_module(charUtils).
final(empty).
final(port).
delta(colon, Char, port) :- isDigitChar(Char), !.
delta(port, Char, port) :- isDigitChar(Char), !.
accept([], port, [], []) :- !.
accept([], empty, ['8', '0'], []) :- !.
accept([/ | Leftover], State, Port, [/ | Leftover]) :-
accept([], State, Port, []),
!.
accept([: | Chars], empty, Port, Leftover) :-
accept(Chars, colon, Port, Leftover),
!.
accept([Char | Chars], State, Port, Leftover) :-
delta(State, Char, NewState),
accept(Chars, NewState, RestPort, Leftover),
append([Char], RestPort, Port).
/**
* portMachine(++Chars:atom[], -Port:atomic, -Leftover:atom[]) is semidet.
*
* True when the list of characters initially has a valid URI port definition.
*/
portMachine(Chars, Port, Leftover) :-
accept(Chars, empty, ValueList, Leftover),
listToURIValue(ValueList, AtomPort),
atom_number(AtomPort, Port).