See: Description
Interface | Description |
---|---|
CharScannerSyntax |
This is the interface used to define the syntax to scan characters.
|
CharStreamScanner |
This is the interface for a scanner that can be used to parse a stream or sequence of characters.
|
javacc
or antlr
. CharStreamScanner
is an interface that covers typical tasks when paring
strings or streams and therefore makes your life a lot easier. You can concentrate on the syntax you want to parse
and do NOT need to repeat checks if the end is already reached all the time. For parsing strings there is the
implementation CharSequenceScanner
that bundles the string together with the
state (parsing position) so you can easily delegate a step to another method or class. Otherwise you would need to
pass the current position to that method and return the new one from there. This is tricky if the method should
already return something else. String input = getInputString(); int i = 0; boolean colonFound = false; while (i < input.length()) { char c = input.charAt(i++); if (c == ':') { colonFound = true; break; } } if (!colonFound) { throw new IllegalArgumentException("Expected character ':' not found!"); } String key = input.substring(0, i - 1); String value = null; if (i < input.length()) { while ((i < input.length()) && (input.charAt(i) == ' ')) { i++; } int start = i; while (i < input.length()) { char c = input.charAt(i); if ((c < '0') || (c > '9')) { break; } i++; } value = input.substring(start, i); }Here is the same thing when using
CharSequenceScanner
:
String input = getInputString();CharStreamScanner
scanner = newCharSequenceScanner
(input); String key = scanner.readUntil
(':', false); if (key == null) { throw new IllegalArgumentException("Expected character ':' not found!"); } scanner.skipWhile
(' '); String value = scanner.readWhile
(CharFilter.LATIN_DIGIT_FILTER
);
Copyright © 2001–2016 mmm-Team. All rights reserved.