forked from ULAnux/mathematica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.pl
81 lines (64 loc) · 1.76 KB
/
lexer.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
% lexer
:- use_module(library(http/dcg_basics)).
lex([H | T]) -->
lexem_t(H), !, % {writeln(H)},
lex(T).
lex([]) --> [].
lexem_t(L) --> trashes, lexem(L), trashes.
trashes --> trash, !, trashes.
trashes --> [].
trash --> comment_marker(End), string(_), End, !.
trash --> white. % elimina blancos
trash --> "\r". % elimina fines de linea
trash --> "\n". % elimina salto de linea
trash --> "\t". % elimina tabuladores
comment_marker("*)") --> "(*".
comment_marker("*/") --> "/*".
comment_marker("\n") --> "%".
comment_marker("\r") --> "%".
comment_marker("") --> "%".
hex_start --> "0X".
hex_start --> "0x".
lexem('(') --> "(".
lexem(')') --> ")".
lexem('[') --> "[".
lexem(']') --> "]".
lexem('{') --> "{".
lexem('}') --> "}".
lexem('.') --> ".".
lexem('+') --> "+".
lexem('-') --> "-".
lexem('*') --> "*".
lexem('/') --> "/".
lexem('^') --> "^".
lexem(',') --> ",".
lexem('!') --> "!".
lexem('?') --> "?".
lexem('¿') --> "¿".
lexem(':') --> ":".
lexem(N) --> hex_start, !, xinteger(N). % this handles hex numbers
lexem(N) --> number(N). % this handles integers/floats
lexem(A) --> identifier_c(L), {string_to_atom(L, A)}.
identifier_c([H | T]) --> alpha(H), !, many_alnum(T).
alpha(H) --> [H], {code_type(H, alpha);code_type(H, csym)}.
alnum(H) --> [H], {code_type(H, alnum);code_type(H, csym)}.
many_alnum([H | T]) --> alnum(H), !, many_alnum(T).
many_alnum([]) --> [].
% test
test_lexem :-
lexem('(', "(", []),
lexem(')' , ")", []),
lexem('[' , "]", []),
lexem('{' , "{", []),
lexem('}' , "}", []),
lexem('.' , ".", []),
lexem('+' , "+", []),
lexem('-' , "-", []),
lexem('*', "*", []),
lexem('/' , "/", []),
lexem('^' , "^", []),
lexem(',' , ",", []),
lexem('!' ,"!", []),
lexem('?' , "?", []),
lexem('¿', "¿", []),
lexem(':', ":", []).