Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 60 additions & 3 deletions compiler/lexer/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ pub fn lexer_parse_file(file_path: &String) -> CompilerResult<Vec<LexerToken>> {
let mut tokens: Vec<LexerToken> = Vec::new();

let mut i: usize = 0;

let mut line: usize = 1;

let mut last_line_break: usize = 0;
while i < contents.len() {
let c: char = contents.chars().nth(i).unwrap();

Expand Down Expand Up @@ -99,6 +97,21 @@ pub fn lexer_parse_file(file_path: &String) -> CompilerResult<Vec<LexerToken>> {
i -= 2; // Try parsing operator as normal token.
}

if c == '/' {
let cc = contents.chars().nth(i + 1).unwrap();
if cc == '/' {
let col = i - last_line_break;

tokens.push(parse_comment(&contents, &mut i, Position::new(file_path.to_string(), line, col))?);
continue;
} else if cc == '.' {
let col = i - last_line_break;

tokens.push(parse_global_comment(&contents, &mut i, Position::new(file_path.to_string(), line, col))?);
continue;
}
}

i += 1;


Expand Down Expand Up @@ -131,6 +144,50 @@ pub fn lexer_parse_file(file_path: &String) -> CompilerResult<Vec<LexerToken>> {
Ok(tokens)
}

fn parse_comment(contents: &String, ind: &mut usize, start_pos: Position) -> CompilerResult<LexerToken> {
*ind += 2;

let start = *ind;
let mut end = start;

for (i, c) in contents[start..].char_indices() {
if c == '\n' || c == '\0' {
end = start + i + c.len_utf8();
break;
}

end = start + i + c.len_utf16();
}

let slice = &contents[*ind + 1..end - 1];

*ind = end;

return Ok(LexerToken::new(start_pos, end - start, LexerTokenType::Comment(slice.to_string())))
}

fn parse_global_comment(contents: &String, ind: &mut usize, start_pos: Position) -> CompilerResult<LexerToken> {
*ind += 2;

let start = *ind;
let mut end = start;

for (i, c) in contents[start..].char_indices() {
if c == '\n' || c == '\0' {
end = start + i + c.len_utf8();
break;
}

end = start + i + c.len_utf16();
}

let slice = &contents[*ind + 1..end - 1];

*ind = end;

return Ok(LexerToken::new(start_pos, end - start, LexerTokenType::GlobalComment(slice.to_string())))
}

fn parse_math_operator(contents: &String, ind: &mut usize, start_pos: Position) -> CompilerResult<LexerToken> {
let operator_char = contents.chars().nth(*ind).unwrap();

Expand Down
3 changes: 3 additions & 0 deletions compiler/lexer/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub enum LexerTokenType {
Function,
ShadowFunction,

Comment(String),
GlobalComment(String),

Var,
Struct,
Layout,
Expand Down