hare

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

ident.ha (1677B)


      1 // License: MPL-2.0
      2 // (c) 2022 Alexey Yerin <yyp@disroot.org>
      3 // (c) 2021 Drew DeVault <sir@cmpwn.com>
      4 use bufio;
      5 use hare::ast;
      6 use hare::lex;
      7 use io;
      8 use io::{mode};
      9 use strings;
     10 
     11 @test fn ident() void = {
     12 	{
     13 		const in = "foo";
     14 		let buf = bufio::fixed(strings::toutf8(in), mode::READ);
     15 		let lexer = lex::init(&buf, "<test>");
     16 		let ident = ident(&lexer) as ast::ident;
     17 		defer ast::ident_free(ident);
     18 		assert(len(ident) == 1);
     19 		assert(ident[0] == "foo");
     20 		let tok = lex::lex(&lexer) as lex::token;
     21 		assert(tok.0 == lex::ltok::EOF);
     22 	};
     23 
     24 	{
     25 		const in = "foo::bar";
     26 		let buf = bufio::fixed(strings::toutf8(in), mode::READ);
     27 		let lexer = lex::init(&buf, "<test>");
     28 		let ident = ident(&lexer) as ast::ident;
     29 		defer ast::ident_free(ident);
     30 		assert(len(ident) == 2);
     31 		assert(ident[0] == "foo" && ident[1] == "bar");
     32 		let tok = lex::lex(&lexer) as lex::token;
     33 		assert(tok.0 == lex::ltok::EOF);
     34 	};
     35 
     36 	{
     37 		const in = "foo::bar::baz";
     38 		let buf = bufio::fixed(strings::toutf8(in), mode::READ);
     39 		let lexer = lex::init(&buf, "<test>");
     40 		let ident = ident(&lexer) as ast::ident;
     41 		defer ast::ident_free(ident);
     42 		assert(len(ident) == 3);
     43 		assert(ident[0] == "foo" && ident[1] == "bar"
     44 			&& ident[2] == "baz");
     45 		let tok = lex::lex(&lexer) as lex::token;
     46 		assert(tok.0 == lex::ltok::EOF);
     47 	};
     48 
     49 	{
     50 		const in = "foo::bar;";
     51 		let buf = bufio::fixed(strings::toutf8(in), mode::READ);
     52 		let lexer = lex::init(&buf, "<test>");
     53 		let ident = ident(&lexer) as ast::ident;
     54 		defer ast::ident_free(ident);
     55 		assert(len(ident) == 2);
     56 		assert(ident[0] == "foo" && ident[1] == "bar");
     57 		let tok = lex::lex(&lexer) as lex::token;
     58 		assert(tok.0 == lex::ltok::SEMICOLON);
     59 	};
     60 };