LexicalAnalyzer
public protocol LexicalAnalyzer : AnyObject
The job of an implementation of this protocol is two fold
- Manage the state of the
Scanner
to provide n lookahead capability to the surrounding rules - To provide a layer of abstraction between consumers of scanning behaviour and the scanner itself. This allows the scanner to be as simple as possible
It used heavily during Rule
evaluation. Typically a rule will start by mark()
ing it’s possition (remembering the positiong of the Scanner
at
the start of rule evaluation. During Rule
evaluation rules will result (eventually) in calls to the various scan
methods which will advance
the Scanner. Finally, the rule will either be satisfied and proceed()->LexicalContext
will be called, or fail and rewind()
will be called returning
the Scanner
to its state before mark()
was called.
-
Creates a new instance of
Lexer
for the suppliedString
.Declaration
Swift
init(source: String)
Parameters
source
The
String
to perform the lexical analysis on -
The character currently being evaluated
Declaration
Swift
var current: String { get }
-
The depth of the look-ahead stack
Declaration
Swift
var depth: Int { get }
-
: Marks the current position. This must be followed by a matching rewind() or proceed()
Declaration
Swift
func mark()
-
Marks the current location and signifies that the scanning is actually skipping
Declaration
Swift
func mark(skipping: Bool)
-
: Discards the current mark, rewinding the scanner to that position
Declaration
Swift
func rewind()
-
: Discards the current mark, but leaving the scanner at it’s current location
Declaration
Swift
func proceed() -> LexicalContext
-
Should return
true
if the end of the sourceString
has been reachedDeclaration
Swift
var endOfInput: Bool { get }
-
Scan until the specified terminal is reached in the source
String
. If the end of theString
is reached before the suppliedString
is matched an error should be thrown. If the terminal is matched, the scanning position should be at the end of that match.See also
scanUpTo(terminal:String)
Declaration
Swift
func scan(terminal: String) throws
Parameters
terminal
A string to scan for
-
Scan until one of the characters in the supplied
CharacterSet
is found. If the end of the source is reached before a character from the set is found anError
should be thrown. The scanner position should be directly after the matched character.Declaration
Swift
func scan(oneOf: CharacterSet) throws
Parameters
oneOf
A
CharacterSet
to scan for -
Scan the supplied regular expression (
NSRegularExpression
). If not found anError
should be the thrown. The scanner position should be directly after the matched pattern.Declaration
Swift
func scan(regularExpression: NSRegularExpression) throws
Parameters
regularExpression
The
NSRegularExpression
to scan for -
Scan up to (that is, the position of the scanner should be at the start of the match, not after it) the supplied
String
. If the terminal is not found anError
should be thrown. The scanner position should be at the first character of the matchedterminal
.See also
scan(terminal:String)
Declaration
Swift
func scanUpTo(terminal: String) throws
Parameters
terminal
A string to scan for
-
Scan up to one of the characters in the supplied
CharacterSet
is found. If the end of the source is reached before a character from the set is found anError
should be thrown. The scanner position should be directly at the matched character.Declaration
Swift
func scanUpTo(oneOf terminal: CharacterSet) throws
Parameters
oneOf
A
CharacterSet
to scan for -
Advances the scanner one character. If the end of the source
String
has already been reached aError
should be thrown.Declaration
Swift
func scanNext() throws
-
Should return the
String
from thesource
representing the supplied range.Declaration
Swift
subscript(range: Range<String.UnicodeScalarView.Index>) -> String { get }
-
Should return the
String
being analyzedDeclaration
Swift
var source: String { get }
-
Should return the integer offset of the current scanning position in charactes
Declaration
Swift
var position: Int { get }
-
Should return the
String.UnicodeScalarView.Index
of the current scanning position in the supplied sourceString
Declaration
Swift
var index: String.UnicodeScalarView.Index { get set }