with allowing commas to be used as the decimal indicator, and spaces
to be used to separate groups of digits in large numbers. Both of
these can reasonably be restricted to appear between two digits, so we
-have to add that condition to our tests.
+have to add that condition to our tests. For consistency we require
+every non-alpha-numeric to appear between two hex digits, with the
+exception that a sign can appear only after a 'p' or 'e', and a space
+can only appear between decimal digits. Allowing a space before a
+letter easily leads to confusion, such a in `a < 3 and b < 4`.
So we cannot just treat numbers as starting with a digit and being
followed by some set of characters. We need more structure than that.
So:
- Numbers must start with a digit.
-- If the first digit is zero, the next character must be a base
- signifier (one of `xob`) or a decimal marker (`.` or `,`).
- In the first case the first `p` or `P` may be followed by a sign.
+- If the first digit is zero, the next character should be a base
+ signifier (one of `xob`) or a decimal marker (`.` or `,`) (though this isn't
+ enforced at this stage)
+ In the first case the only first `p` or `P` may be followed by a sign.
- If the number doesn't start with `0` followed by one of `xob`, the
first `e` may be followed by a sign.
-- Any digit or hex digit may be followed by a space or underscore
- providing that the subsequence character is also a (hex) digit.
+- A sign must always be followed by a digit.
+- Any digit may be followed by a space or underscore and any hex digit
+ maybe followed by an underscore, providing that the subsequence character
+ is also a digit (for space) or hex digit (for underscore).
This rule will require an extra level of 'unget' to be
supported when handling characters.
- Otherwise any digits or ASCII letters are allowed. We do not at
###### parse number
if (iswdigit(ch) && !(ignored & (1<<TK_number))) {
- int prev_special = 0;
+ int prev = 0;
int expect_p = 0;
int decimal_mark = 0;
if (ch == '0') {
int sign_ok = 0;
switch(expect_p) {
case 0:
- if (ch == 'e' || ch == 'E')
+ if (ch == 'e' || ch == 'E') {
sign_ok = 1;
+ decimal_mark = 1;
+ }
break;
case 1:
- if (ch == 'p' || ch == 'P')
+ if (ch == 'p' || ch == 'P') {
sign_ok = 1;
+ decimal_mark = 1;
+ }
break;
}
save_unget_state(state);
+ prev = ch;
ch = get_char(state);
- if (iswalnum(ch)) {
- prev_special = 0;
+
+ if (!iswalnum(prev)) {
+ /* special characters, like separators and decimal marks
+ * and signs, must be followed by a hexdigit, and the
+ * space and signs must be followed by a decimal digit.
+ */
+ if (!iswxdigit(ch) ||
+ ((prev == '-' || prev == '+') && !iswdigit(ch)) ||
+ (prev == ' ' && !iswdigit(ch))) {
+ /* don't want the new char or the special */
+ restore_unget_state(state);
+ break;
+ }
+ }
+ if (iswalnum(ch))
continue;
+
+ if (!strchr(state->conf->number_chars, ch)) {
+ /* non-number char */
+ break;
}
if (ch == '+' || ch == '-') {
+ /* previous must be 'e' or 'p' in appropraite context */
if (!sign_ok)
break;
expect_p = -1;
+ } else if (ch == ' ') {
+ /* previous must be a digit */
+ if (!iswdigit(prev))
+ break;
+ } else {
+ /* previous must be a hex digit */
+ if (!iswxdigit(prev))
+ break;
}
if (ch == '.' || ch == ',') {
+ /* only one of these permitted */
if (decimal_mark)
break;
decimal_mark = 1;
}
- if (prev_special) {
- /* Don't allow that special char,
- * need two 'ungets'
- */
- restore_unget_state(state);
- break;
- }
- if (strchr(state->conf->number_chars, ch)) {
- prev_special = 1;
- continue;
- }
- /* non-number char */
- break;
}
/* We seem to have a "number" token */
unget_char(state);
Converting a `TK_number` token to a numerical value is a slightly
higher level task than lexical analysis, and slightly lower than
-grammar parsing, so put it here - as an index if you like.
+grammar parsing, so put it here - as an appendix if you like.
Importantly it will be used by the same testing rig that is used for
testing the token scanner.
int *placesp)
{
/* Accept digits up to 'base', ignore '_' and
- * ' ' if they appear between two legal digits,
- * and if `placesp` is not NULL, allow a single
- * '.' or ',' and report the number of digits
- * beyond there.
+ * (for base 10) ' ' if they appear between two
+ * legal digits, and if `placesp` is not NULL,
+ * allow a single '.' or ',' and report the number
+ * of digits beyond there.
* Return number of characters processed (p),
* or 0 if something illegal was found.
*/
int dig;
char c = tok.txt[p];
- if (c == '_' || c == ' ') {
+ if (c == '_' || (c == ' ' && base == 10)) {
if (prev != Digit)
goto bad;
prev = Space;
places until we have the exponent as well.
###### number vars
- int places =0;
+ int places = 0;
mpz_t mant;
int d;