hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

lexer.ha (1419B)


      1 use bufio;
      2 use strings;
      3 use io;
      4 
      5 @test fn lex() void = {
      6 	const cases: [_](str, []token) = [
      7 		("true", [true]),
      8 		("false", [false]),
      9 		("null", [_null]),
     10 		("1234", [1234.0]),
     11 		("12.34", [12.34]),
     12 		("12.34e5", [12.34e5]),
     13 		("12.34E5", [12.34e5]),
     14 		("12.34e+5", [12.34e5]),
     15 		("12.34e-5", [12.34e-5]),
     16 		("12e5", [12.0e5]),
     17 		("-1234", [-1234.0]),
     18 		(`"hello world"`, ["hello world"]),
     19 		(`"\"\\\/\b\f\n\r\t\u0020"`, ["\"\\/\b\f\n\r\t\u0020"]),
     20 		("[ null, null ]", [arraystart, _null, comma, _null, arrayend]),
     21 	];
     22 
     23 	for (let i = 0z; i < len(cases); i += 1) {
     24 		const src = strings::toutf8(cases[i].0);
     25 		const src = bufio::fixed(src, io::mode::READ);
     26 		const lexer = newlexer(&src);
     27 		defer close(&lexer);
     28 
     29 		for (let j = 0z; j < len(cases[i].1); j += 1) {
     30 			const want = cases[i].1[j];
     31 			const have = lex(&lexer)! as token;
     32 			assert(tokeq(want, have));
     33 		};
     34 
     35 		assert(lex(&lexer) is io::EOF);
     36 	};
     37 };
     38 
     39 fn tokeq(want: token, have: token) bool = {
     40 	match (want) {
     41 	case _null =>
     42 		return have is _null;
     43 	case comma =>
     44 		return have is comma;
     45 	case colon =>
     46 		return have is colon;
     47 	case arraystart =>
     48 		return have is arraystart;
     49 	case arrayend =>
     50 		return have is arrayend;
     51 	case objstart =>
     52 		return have is objstart;
     53 	case objend =>
     54 		return have is objend;
     55 	case let b: bool =>
     56 		return have as bool == b;
     57 	case let f: f64 =>
     58 		return have as f64 == f;
     59 	case let s: str =>
     60 		return have as str == s;
     61 	};
     62 };