adjusted examples to use ->

This commit is contained in:
cozis
2022-12-08 03:15:05 +01:00
parent f7d28556a1
commit 57b4f37d3a
4 changed files with 166 additions and 157 deletions
+67 -71
View File
@@ -2,23 +2,19 @@ scan = import("scan.noja");
newScanner = scan.newScanner;
Scanner = scan.Scanner;
hint = scan.hint;
consume = scan.consume;
current = scan.current;
consumeSpaces = scan.consumeSpaces;
isDigit = scan.isDigit;
fun parseString(scan: Scanner) {
assert(current(scan) == '"');
consume(scan); # Consume the opening "
assert(scan->current() == '"');
scan->consume(); # Consume the opening "
temp = "";
char = current(scan);
char = scan->current();
while char != '"' and char != none: {
temp = string.cat(temp, char);
consume(scan);
char = current(scan);
scan->consume();
char = scan->current();
}
if char == none: {
@@ -26,7 +22,7 @@ fun parseString(scan: Scanner) {
err = "Source ended inside a string literal";
} else {
assert(char == '"');
consume(scan);
scan->consume();
val = temp;
err = none;
}
@@ -36,22 +32,22 @@ fun parseString(scan: Scanner) {
fun parseObject(scan: Scanner) {
assert(current(scan) == '{');
consume(scan);
assert(scan->current() == '{');
scan->consume();
obj = {};
consumeSpaces(scan);
if current(scan) != '}': do {
scan->consumeSpaces();
if scan->current() != '}': do {
key, err = parseValue(scan);
if err != none:
return none, err;
if type(key) != String:
return none, "Object key isn't a string";
consumeSpaces(scan);
if current(scan) != ':':
scan->consumeSpaces();
if scan->current() != ':':
return none, "Missing ':' separator after key";
consume(scan);
scan->consume();
val, err = parseValue(scan);
if err != none:
@@ -61,111 +57,111 @@ fun parseObject(scan: Scanner) {
return none, "Duplicate key in object";
obj[key] = val;
consumeSpaces(scan);
if current(scan) == '}':
scan->consumeSpaces();
if scan->current() == '}':
done = true;
else {
if current(scan) != ',':
if scan->current() != ',':
return none, "Invalid character where either ',' or '}' were expected";
consume(scan); # Consume the ","
scan->consume(); # Consume the ","
done = false;
}
} while not done;
assert(current(scan) == '}');
consume(scan);
assert(scan->current() == '}');
scan->consume();
return obj;
}
fun parseArray(scan: Scanner) {
assert(current(scan) == '[');
consume(scan); # Consume the [
assert(scan->current() == '[');
scan->consume(); # Consume the [
array = [];
consumeSpaces(scan);
if current(scan) != ']': do {
scan->consumeSpaces();
if scan->current() != ']': do {
val, err = parseValue(scan);
if err != none:
return none, err;
array[count(array)] = val;
consumeSpaces(scan);
if current(scan) == ']':
scan->consumeSpaces();
if scan->current() == ']':
done = true;
else {
if current(scan) != ',':
if scan->current() != ',':
return none, "Invalid character where either ',' or ']' were expected";
consume(scan); # Consume the ","
scan->consume(); # Consume the ","
done = false;
}
} while not done;
assert(current(scan) == ']');
consume(scan);
assert(scan->current() == ']');
scan->consume();
return array;
}
fun parseFalse(scan: Scanner) {
assert(current(scan) == 'f');
consume(scan);
assert(scan->current() == 'f');
scan->consume();
if current(scan) != 'a':
if scan->current() != 'a':
return none, "Invalid character after 'f'";
consume(scan);
scan->consume();
if current(scan) != 'l':
if scan->current() != 'l':
return none, "Invalid character after 'fa'";
consume(scan);
scan->consume();
if current(scan) != 's':
if scan->current() != 's':
return none, "Invalid character after 'fal'";
consume(scan);
scan->consume();
if current(scan) != 'e':
if scan->current() != 'e':
return none, "Invalid character after 'fals'";
consume(scan);
scan->consume();
return false;
}
fun parseTrue(scan: Scanner) {
assert(current(scan) == 't');
consume(scan);
assert(scan->current() == 't');
scan->consume();
if current(scan) != 'r':
if scan->current() != 'r':
return none, "Invalid character after 't'";
consume(scan);
scan->consume();
if current(scan) != 'u':
if scan->current() != 'u':
return none, "Invalid character after 'tr'";
consume(scan);
scan->consume();
if current(scan) != 'e':
if scan->current() != 'e':
return none, "Invalid character after 'tru'";
consume(scan);
scan->consume();
return true;
}
fun parseNull(scan: Scanner) {
assert(current(scan) == 'n');
consume(scan);
assert(scan->current() == 'n');
scan->consume();
if current(scan) != 'u':
if scan->current() != 'u':
return none, "Invalid character after 'n'";
consume(scan);
scan->consume();
if current(scan) != 'l':
if scan->current() != 'l':
return none, "Invalid character after 'nu'";
consume(scan);
scan->consume();
if current(scan) != 'l':
if scan->current() != 'l':
return none, "Invalid character after 'nul'";
consume(scan);
scan->consume();
return none;
}
@@ -178,15 +174,15 @@ fun integerFromDigit(char: String) {
fun parseNumber(scan: Scanner) {
char = current(scan);
char = scan->current();
sign = 1;
if char == '+' or char == '-': {
sign = {'+': 1, '-': -1}[char];
consume(scan);
scan->consume();
char = current(scan);
char = scan->current();
if not isDigit(char):
return none, "Invalid character after +/-";
}
@@ -198,23 +194,23 @@ fun parseNumber(scan: Scanner) {
n = integerFromDigit(char);
temp = temp * 10 + n;
consume(scan);
char = current(scan);
scan->consume();
char = scan->current();
} while isDigit(char);
if char == '.' and isDigit(hint(scan, 1)): {
if char == '.' and isDigit(scan->hint(1)): {
consume(scan); # Consume the dot
char = current(scan);
scan->consume(); # Consume the dot
char = scan->current();
q = 1.0;
do {
q = q / 10;
n = integerFromDigit(char);
temp = temp + q * n;
consume(scan);
char = current(scan);
scan->consume();
char = scan->current();
} while isDigit(char);
}
@@ -244,8 +240,8 @@ fun parseValue(scan: Scanner) {
'9': parseNumber
};
consumeSpaces(scan);
char = current(scan);
scan->consumeSpaces();
char = scan->current();
if char == none: {
val = none;
err = "Source ended where a value was expected";