From d55bea7c46500f02521ef16ee7434b89380524fa Mon Sep 17 00:00:00 2001 From: WerWolv Date: Fri, 27 Nov 2020 14:18:28 +0100 Subject: [PATCH] Added character literals to pattern language --- source/lang/lexer.cpp | 31 +++++++++++++++++++++++++++++++ source/views/view_pattern.cpp | 2 ++ 2 files changed, 33 insertions(+) diff --git a/source/lang/lexer.cpp b/source/lang/lexer.cpp index ee18918d8..61aaf5f3e 100644 --- a/source/lang/lexer.cpp +++ b/source/lang/lexer.cpp @@ -131,6 +131,37 @@ namespace hex::lang { } else if (c == '*') { tokens.push_back({ .type = Token::Type::Operator, .operatorToken = { .op = Token::OperatorToken::Operator::Star } }); offset += 1; + } else if (c == '\'') { + offset += 1; + + if (offset >= code.length()) + return { ResultLexicalError, { } }; + + char character = code[offset]; + + if (character == '\\') { + offset += 1; + + if (offset >= code.length()) + return { ResultLexicalError, { } }; + + if (code[offset] != '\\' && code[offset] != '\'') + return { ResultLexicalError, { } }; + + character = code[offset]; + } else { + if (code[offset] == '\\' || code[offset] == '\'') + return { ResultLexicalError, { } }; + } + + offset += 1; + + if (offset >= code.length() || code[offset] != '\'') + return { ResultLexicalError, { } }; + + tokens.push_back({ .type = Token::Type::Integer, .integerToken = { .integer = character } }); + offset += 1; + } else if (std::isalpha(c)) { std::string identifier = matchTillInvalid(&code[offset], [](char c) -> bool { return std::isalnum(c) || c == '_'; }); diff --git a/source/views/view_pattern.cpp b/source/views/view_pattern.cpp index b8e7257b2..7d12b20fa 100644 --- a/source/views/view_pattern.cpp +++ b/source/views/view_pattern.cpp @@ -48,6 +48,8 @@ namespace hex { paletteIndex = TextEditor::PaletteIndex::Identifier; else if (TokenizeCStyleNumber(inBegin, inEnd, outBegin, outEnd)) paletteIndex = TextEditor::PaletteIndex::Number; + else if (TokenizeCStyleCharacterLiteral(inBegin, inEnd, outBegin, outEnd)) + paletteIndex = TextEditor::PaletteIndex::CharLiteral; return paletteIndex != TextEditor::PaletteIndex::Max; };