annotate trunk/src/dil/lexer/Funcs.d @ 577:9e811db780a6

Moved LexerFuncs.d to package 'lexer'.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Sat, 05 Jan 2008 17:01:15 +0100
parents trunk/src/dil/LexerFuncs.d@2a8d0ed0d71e
children b6c6baa41267
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
490
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
1 /++
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
2 Author: Aziz Köksal
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
3 License: GPL3
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
4 +/
577
9e811db780a6 Moved LexerFuncs.d to package 'lexer'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 533
diff changeset
5 module dil.lexer.Funcs;
490
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
6
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
7 const char[3] LS = \u2028; /// Line separator.
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
8 const char[3] PS = \u2029; /// Paragraph separator.
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
9 const dchar LSd = 0x2028;
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
10 const dchar PSd = 0x2029;
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
11 static assert(LS[0] == PS[0] && LS[1] == PS[1]);
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
12
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
13 const uint _Z_ = 26; /// Control+Z
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
14
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
15 /// Returns true if d is a Unicode line or paragraph separator.
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
16 bool isUnicodeNewlineChar(dchar d)
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
17 {
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
18 return d == LSd || d == PSd;
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
19 }
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
20
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
21 /// Returns true if p points to a line or paragraph separator.
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
22 bool isUnicodeNewline(char* p)
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
23 {
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
24 return *p == LS[0] && p[1] == LS[1] && (p[2] == LS[2] || p[2] == PS[2]);
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
25 }
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
26
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
27 /++
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
28 Returns true if p points to the start of a Newline.
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
29 Newline: \n | \r | \r\n | LS | PS
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
30 +/
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
31 bool isNewline(char* p)
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
32 {
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
33 return *p == '\n' || *p == '\r' || isUnicodeNewline(p);
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
34 }
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
35
533
2a8d0ed0d71e Improved error reporting in dil.Converter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 496
diff changeset
36 /// Returns if c is a Newline character.
2a8d0ed0d71e Improved error reporting in dil.Converter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 496
diff changeset
37 bool isNewline(dchar c)
2a8d0ed0d71e Improved error reporting in dil.Converter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 496
diff changeset
38 {
2a8d0ed0d71e Improved error reporting in dil.Converter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 496
diff changeset
39 return c == '\n' || c == '\r' || isUnicodeNewlineChar(c);
2a8d0ed0d71e Improved error reporting in dil.Converter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 496
diff changeset
40 }
2a8d0ed0d71e Improved error reporting in dil.Converter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 496
diff changeset
41
490
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
42 /++
496
5a607597dc22 Improved error recovery in the Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 494
diff changeset
43 Returns true if p points to an EOF character.
5a607597dc22 Improved error recovery in the Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 494
diff changeset
44 EOF: 0 | _Z_
5a607597dc22 Improved error recovery in the Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 494
diff changeset
45 +/
5a607597dc22 Improved error recovery in the Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 494
diff changeset
46 bool isEOF(dchar c)
5a607597dc22 Improved error recovery in the Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 494
diff changeset
47 {
5a607597dc22 Improved error recovery in the Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 494
diff changeset
48 return c == 0 || c == _Z_;
5a607597dc22 Improved error recovery in the Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 494
diff changeset
49 }
5a607597dc22 Improved error recovery in the Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 494
diff changeset
50
5a607597dc22 Improved error recovery in the Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 494
diff changeset
51 /++
490
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
52 Returns true if p points to the first character of an EndOfLine.
496
5a607597dc22 Improved error recovery in the Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 494
diff changeset
53 EndOfLine: Newline | EOF
490
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
54 +/
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
55 bool isEndOfLine(char* p)
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
56 {
496
5a607597dc22 Improved error recovery in the Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 494
diff changeset
57 return isNewline(p) || isEOF(*p);
490
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
58 }
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
59
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
60 /++
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
61 Scans a Newline and sets p one character past it.
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
62 Returns '\n' if scanned or 0 otherwise.
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
63 +/
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
64 dchar scanNewline(ref char* p)
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
65 {
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
66 switch (*p)
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
67 {
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
68 case '\r':
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
69 if (p[1] == '\n')
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
70 ++p;
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
71 case '\n':
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
72 ++p;
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
73 return '\n';
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
74 default:
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
75 if (isUnicodeNewline(p))
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
76 {
494
9a7ca8c56e59 Refactored a few things in the Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 490
diff changeset
77 p += 3;
490
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
78 return '\n';
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
79 }
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
80 }
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
81 return 0;
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
82 }