Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
Implement concat with a temporary lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
hdamron17 committed Aug 17, 2020
1 parent 28aaca8 commit 1f32b12
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/data/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,10 @@ pub enum CppError {
/// '##' missing arguments
#[error("'##' cannot appear at {} of macro expansion", if *(.0) { "start" } else { "end"})]
HashHashMissingParameter(bool),

/// The result of '##' is not a valid token
#[error("pasting formed '{0}{1}', an invalid preprocessing token")]
HashHashInvalid(Token, Token),
}

/// Lex errors are non-exhaustive and may have new variants added at any time
Expand Down
21 changes: 17 additions & 4 deletions src/lex/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! This module does no parsing and accepts only tokens.
use super::{cpp::CppResult, files::FileProcessor};
use super::{cpp::CppResult, files::FileProcessor, Lexer};
use crate::{
error::CppError, CompileError, CompileResult, InternedStr, LiteralToken, Locatable, Location,
Token,
Expand Down Expand Up @@ -183,7 +183,16 @@ pub fn replace(
continue;
}
let pending_hashhash = pending_hashhash.take().unwrap(); // We just checked that it's some
let concat_token = concat(pending_hashhash, succeeding_tok.clone(), &location);
let concat_token =
concat(&pending_hashhash, succeeding_tok, &location).ok_or_else(|| {
location.with(
CppError::HashHashInvalid(
pending_hashhash.clone(),
succeeding_tok.clone(),
)
.into(),
)
});
replacements.push(concat_token); // TODO don't bypass pending
continue;
}
Expand Down Expand Up @@ -480,8 +489,12 @@ fn stringify(args: Vec<Token>) -> Token {
))]))
}

fn concat(x: Token, b: Token, location: &Location) -> CompileResult<Locatable<Token>> {
todo!();
fn concat(x: &Token, y: &Token, location: &Location) -> Option<Locatable<Token>> {
let mut lexer = Lexer::new(location.file, format!("{}{}", x, y), false);
match lexer.next() {
Some(Ok(tok)) if lexer.next().is_none() => Some(tok),
_ => None,
}
}

fn wrap_error(location: &Location, err: CppError) -> Vec<CppResult<Token>> {
Expand Down

0 comments on commit 1f32b12

Please sign in to comment.