From a5f3f1b046bf4bd88cc0fb98af9d3409efd5fc31 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Fri, 15 Nov 2019 13:21:12 +0100 Subject: [PATCH] extend lexer with some needed symbols --- alib2cli/src/lexer/Lexer.cpp | 56 +++++++++++++++++++++++++++++++--- alib2cli/src/lexer/Lexer.h | 38 ++++++++++++++++++----- alib2cli/src/parser/Parser.cpp | 18 +++++------ 3 files changed, 92 insertions(+), 20 deletions(-) diff --git a/alib2cli/src/lexer/Lexer.cpp b/alib2cli/src/lexer/Lexer.cpp index f6504a6b0a..732a3b97b0 100644 --- a/alib2cli/src/lexer/Lexer.cpp +++ b/alib2cli/src/lexer/Lexer.cpp @@ -32,13 +32,13 @@ q0: if ( m_source.getCharacter ( ) == EOF ) { if ( m_source.getCharacter ( ) == '<' ) { res.m_raw += m_source.getCharacter ( ); m_source.advance ( readNextLine ); - res.m_type = TokenType::IN_REDIRECT; + res.m_type = TokenType::LESS_SIGN; return res; } if ( m_source.getCharacter ( ) == '>' ) { res.m_raw += m_source.getCharacter ( ); m_source.advance ( readNextLine ); - res.m_type = TokenType::OUT_REDIRECT; + res.m_type = TokenType::MORE_SIGN; return res; } if ( m_source.getCharacter ( ) == '(' ) { @@ -131,12 +131,60 @@ q0: if ( m_source.getCharacter ( ) == EOF ) { res.m_type = TokenType::HASH_SIGN; return res; } + if ( m_source.getCharacter ( ) == ',' ) { + res.m_raw += m_source.getCharacter ( ); + m_source.advance ( readNextLine ); + res.m_type = TokenType::COMMA; + return res; + } if ( m_source.getCharacter ( ) == '-' ) { res.m_raw += m_source.getCharacter ( ); res.m_value += m_source.getCharacter ( ); m_source.advance ( readNextLine ); goto q2; } + if ( m_source.getCharacter ( ) == '+' ) { + res.m_raw += m_source.getCharacter ( ); + m_source.advance ( readNextLine ); + res.m_type = TokenType::PLUS_SIGN; + return res; + } + if ( m_source.getCharacter ( ) == '/' ) { + res.m_raw += m_source.getCharacter ( ); + m_source.advance ( readNextLine ); + res.m_type = TokenType::SLASH_SIGN; + return res; + } + if ( m_source.getCharacter ( ) == '*' ) { + res.m_raw += m_source.getCharacter ( ); + m_source.advance ( readNextLine ); + res.m_type = TokenType::STAR_SIGN; + return res; + } + if ( m_source.getCharacter ( ) == '~' ) { + res.m_raw += m_source.getCharacter ( ); + m_source.advance ( readNextLine ); + res.m_type = TokenType::TYLDE_SIGN; + return res; + } + if ( m_source.getCharacter ( ) == '!' ) { + res.m_raw += m_source.getCharacter ( ); + m_source.advance ( readNextLine ); + res.m_type = TokenType::EXCLAMATION_SIGN; + return res; + } + if ( m_source.getCharacter ( ) == '%' ) { + res.m_raw += m_source.getCharacter ( ); + m_source.advance ( readNextLine ); + res.m_type = TokenType::PERCENTAGE_SIGN; + return res; + } + if ( m_source.getCharacter ( ) == '.' ) { + res.m_raw += m_source.getCharacter ( ); + m_source.advance ( readNextLine ); + res.m_type = TokenType::DOT; + return res; + } if ( m_source.getCharacter ( ) == '"' ) { res.m_raw += m_source.getCharacter ( ); @@ -185,7 +233,7 @@ q1: if ( m_source.getCharacter ( ) == '\0' ) { q2: if ( m_source.getCharacter ( ) == '\0' ) { res.m_value = ""; - res.m_type = TokenType::DASH_SIGN; + res.m_type = TokenType::MINUS_SIGN; return res; } if ( ( m_source.getCharacter ( ) >= '0' && m_source.getCharacter ( ) <= '9' ) ) { @@ -209,7 +257,7 @@ q2: if ( m_source.getCharacter ( ) == '\0' ) { } res.m_value = ""; - res.m_type = TokenType::DASH_SIGN; + res.m_type = TokenType::MINUS_SIGN; return res; q3: if ( m_source.getCharacter ( ) == '\0' ) { diff --git a/alib2cli/src/lexer/Lexer.h b/alib2cli/src/lexer/Lexer.h index 2b76413007..3e796624ec 100644 --- a/alib2cli/src/lexer/Lexer.h +++ b/alib2cli/src/lexer/Lexer.h @@ -30,8 +30,8 @@ public: IDENTIFIER, INTEGER, STRING, - IN_REDIRECT, - OUT_REDIRECT, + LESS_SIGN, + MORE_SIGN, LEFT_PAREN, RIGHT_PAREN, LEFT_BRACE, @@ -45,9 +45,17 @@ public: CARET_SIGN, COLON_SIGN, SEMICOLON_SIGN, - DASH_SIGN, + MINUS_SIGN, + PLUS_SIGN, + SLASH_SIGN, + STAR_SIGN, + TYLDE_SIGN, + EXCLAMATION_SIGN, + PERCENTAGE_SIGN, EQUAL_SIGN, HASH_SIGN, + COMMA, + DOT, FILE, TYPE, ERROR, @@ -67,9 +75,9 @@ public: return "number"; case TokenType::STRING : return "string"; - case TokenType::IN_REDIRECT : + case TokenType::LESS_SIGN : return "in_redirect"; - case TokenType::OUT_REDIRECT : + case TokenType::MORE_SIGN : return "out_redirect"; case TokenType::LEFT_PAREN : return "left_paren"; @@ -97,12 +105,28 @@ public: return "colon_sign"; case TokenType::SEMICOLON_SIGN : return "semicolon_sign"; - case TokenType::DASH_SIGN : - return "dash_sign"; + case TokenType::MINUS_SIGN : + return "minus_sign"; + case TokenType::PLUS_SIGN : + return "plus_sign"; + case TokenType::SLASH_SIGN : + return "slash_sign"; + case TokenType::STAR_SIGN : + return "star_sign"; + case TokenType::TYLDE_SIGN : + return "tylde_sign"; + case TokenType::EXCLAMATION_SIGN : + return "exclemation_sign"; + case TokenType::PERCENTAGE_SIGN : + return "percentage_sign"; case TokenType::EQUAL_SIGN : return "equal_sign"; case TokenType::HASH_SIGN : return "hash_sign"; + case TokenType::COMMA : + return "comma"; + case TokenType::DOT : + return "dot"; case TokenType::FILE : return "file"; case TokenType::TYPE : diff --git a/alib2cli/src/parser/Parser.cpp b/alib2cli/src/parser/Parser.cpp index f7e56a925b..df6beb086b 100644 --- a/alib2cli/src/parser/Parser.cpp +++ b/alib2cli/src/parser/Parser.cpp @@ -174,8 +174,8 @@ std::shared_ptr < Statement > Parser::common ( ) { match ( cli::Lexer::TokenType::DOLAR_SIGN ); std::unique_ptr < Arg > name = arg ( ); return std::make_shared < VariableStatement > ( std::move ( name ) ); - } else if ( check ( cli::Lexer::TokenType::IN_REDIRECT ) ) { - match ( cli::Lexer::TokenType::IN_REDIRECT ); + } else if ( check ( cli::Lexer::TokenType::LESS_SIGN ) ) { + match ( cli::Lexer::TokenType::LESS_SIGN ); return in_redirect ( ); } else if ( check ( cli::Lexer::TokenType::STRING ) ) { std::string value = matchString ( ); @@ -208,10 +208,10 @@ std::shared_ptr < Statement > Parser::common ( ) { } std::shared_ptr < Statement > Parser::param ( ) { - if ( check ( cli::Lexer::TokenType::DOLAR_SIGN, cli::Lexer::TokenType::IN_REDIRECT, cli::Lexer::TokenType::STRING, cli::Lexer::TokenType::INTEGER, cli::Lexer::TokenType::HASH_SIGN, cli::Lexer::TokenType::LEFT_BRACE ) ) { + if ( check ( cli::Lexer::TokenType::DOLAR_SIGN, cli::Lexer::TokenType::LESS_SIGN, cli::Lexer::TokenType::STRING, cli::Lexer::TokenType::INTEGER, cli::Lexer::TokenType::HASH_SIGN, cli::Lexer::TokenType::LEFT_BRACE ) ) { return common ( ); - } else if ( check ( cli::Lexer::TokenType::DASH_SIGN ) ) { - match ( cli::Lexer::TokenType::DASH_SIGN ); + } else if ( check ( cli::Lexer::TokenType::MINUS_SIGN ) ) { + match ( cli::Lexer::TokenType::MINUS_SIGN ); return std::make_shared < PreviousResultStatement > ( ); } else if ( check ( cli::Lexer::TokenType::IDENTIFIER ) ) { std::string value = matchIdentifier ( ); @@ -229,7 +229,7 @@ std::shared_ptr < Statement > Parser::param ( ) { } std::shared_ptr < Statement > Parser::statement ( ) { - if ( check ( cli::Lexer::TokenType::DOLAR_SIGN, cli::Lexer::TokenType::IN_REDIRECT, cli::Lexer::TokenType::STRING, cli::Lexer::TokenType::INTEGER, cli::Lexer::TokenType::HASH_SIGN, cli::Lexer::TokenType::LEFT_BRACE ) ) { + if ( check ( cli::Lexer::TokenType::DOLAR_SIGN, cli::Lexer::TokenType::LESS_SIGN, cli::Lexer::TokenType::STRING, cli::Lexer::TokenType::INTEGER, cli::Lexer::TokenType::HASH_SIGN, cli::Lexer::TokenType::LEFT_BRACE ) ) { return common ( ); } else if ( check ( cli::Lexer::TokenType::IDENTIFIER ) ) { std::unique_ptr < Arg > name = std::make_unique < ImmediateArg > ( matchIdentifier ( ) ); @@ -241,7 +241,7 @@ std::shared_ptr < Statement > Parser::statement ( ) { std::unique_ptr < CategoryOption > category = category_option ( ); ext::vector < std::shared_ptr < Statement > > params; ext::vector < bool > moves; - while ( ! check ( cli::Lexer::TokenType::OUT_REDIRECT, cli::Lexer::TokenType::PIPE_SIGN, cli::Lexer::TokenType::EOS, cli::Lexer::TokenType::EOT, cli::Lexer::TokenType::RIGHT_PAREN, cli::Lexer::TokenType::SEMICOLON_SIGN ) ) { + while ( ! check ( cli::Lexer::TokenType::MORE_SIGN, cli::Lexer::TokenType::PIPE_SIGN, cli::Lexer::TokenType::EOS, cli::Lexer::TokenType::EOT, cli::Lexer::TokenType::RIGHT_PAREN, cli::Lexer::TokenType::SEMICOLON_SIGN ) ) { moves.push_back ( move_arg ( ) ); params.emplace_back ( param ( ) ); } @@ -295,8 +295,8 @@ void Parser::out_redirect ( std::shared_ptr < StatementList > & list ) { } void Parser::result ( std::shared_ptr < StatementList > & list ) { - if ( check ( cli::Lexer::TokenType::OUT_REDIRECT ) ) { - match ( cli::Lexer::TokenType::OUT_REDIRECT ); + if ( check ( cli::Lexer::TokenType::MORE_SIGN ) ) { + match ( cli::Lexer::TokenType::MORE_SIGN ); out_redirect ( list ); } else if ( check ( cli::Lexer::TokenType::EOS, cli::Lexer::TokenType::EOT, cli::Lexer::TokenType::SEMICOLON_SIGN ) ) { list->append ( std::make_unique < ResultPrintStatement > ( ) ); -- GitLab