1 // =================================
 2 // Copyright (c) 2020 Seppo Laakko
 3 // Distributed under the MIT license
 4 // =================================
 5 
 6 [hpp]#include <sngcpp/parser/ParserApi.hpp>
 7 [hpp]#include <sngcpp/parser/TokenValueParsers.hpp>
 8 [hpp]#include <sngcpp/ast/Literal.hpp>
 9 [cpp]#include <sngcpp/lexer/CppLexer.hpp>
10 [cpp]#include <sngcpp/lexer/CppTokens.hpp>
11 
12 using namespace CppTokens;
13 
14 parser api(SNGCPP_PARSER_API) LiteralParser
15 {
16     uselexer CppLexer;
17 
18     Literal : sngcpp::ast::LiteralNode*
19         ::= FLOATLIT
20             {
21                 soulng::lexer::Token token = lexer.GetToken(pos);
22                 double value = 0.0;
23                 sngcpp::ast::Suffix suffix = sngcpp::ast::Suffix::none;
24                 sngcpp::cppparser::ParseFloatingLiteral(lexer.FileName(), token, value, suffix);
25                 return new sngcpp::ast::FloatingLiteralNode(span, value, suffix, token.match.ToString());
26             }
27         |   INTLIT
28             {
29                 soulng::lexer::Token token = lexer.GetToken(pos);
30                 uint64_t value = 0;
31                 sngcpp::ast::Base base = sngcpp::ast::Base::decimal;
32                 sngcpp::ast::Suffix suffix = sngcpp::ast::Suffix::none;
33                 sngcpp::cppparser::ParseIntegerLiteral(lexer.FileName(), token, value, base, suffix);
34                 return new sngcpp::ast::IntegerLiteralNode(span, value, suffix, base, token.match.ToString());
35             }
36         |   CHARLIT
37             {
38                 soulng::lexer::Token token = lexer.GetToken(pos);
39                 char32_t value = '\0';
40                 char32_t prefix = '\0';
41                 sngcpp::cppparser::ParseCharacterLiteral(lexer.FileName(), token, value, prefix);
42                 return new sngcpp::ast::CharacterLiteralNode(span, prefix, value, token.match.ToString());
43             }
44         |   STRINGLIT
45             {
46                 soulng::lexer::Token token = lexer.GetToken(pos);
47                 std::u32string value;
48                 std::u32string encodingPrefix;
49                 sngcpp::cppparser::ParseStringLiteral(lexer.FileName(), token, encodingPrefix, value);
50                 return new sngcpp::ast::StringLiteralNode(span, encodingPrefix, value, token.match.ToString());
51             }
52         |   TRUE
53             {
54                 soulng::lexer::Token token = lexer.GetToken(pos);
55                 return new sngcpp::ast::BooleanLiteralNode(span, true, token.match.ToString());
56             }
57         |   FALSE
58             {
59                 soulng::lexer::Token token = lexer.GetToken(pos);
60                 return new sngcpp::ast::BooleanLiteralNode(span, false, token.match.ToString());
61             }
62         |   NULLPTR
63             {
64                 soulng::lexer::Token token = lexer.GetToken(pos);
65                 return new sngcpp::ast::NullPtrLiteralNode(span, token.match.ToString());
66             }
67         ;
68 
69     StringLiteral : sngcpp::ast::StringLiteralNode*
70         ::= STRINGLIT
71         {
72             soulng::lexer::Token token = lexer.GetToken(pos);
73             std::u32string value;
74             std::u32string encodingPrefix;
75             sngcpp::cppparser::ParseStringLiteral(lexer.FileName(), token, encodingPrefix, value);
76             return new sngcpp::ast::StringLiteralNode(span, encodingPrefix, value, token.match.ToString());
77         }
78         ;
79 
80     IntegerLiteral : sngcpp::ast::IntegerLiteralNode*
81         ::= INTLIT
82         {
83             soulng::lexer::Token token = lexer.GetToken(pos);
84             uint64_t value = 0;
85             sngcpp::ast::Base base = sngcpp::ast::Base::decimal;
86             sngcpp::ast::Suffix suffix = sngcpp::ast::Suffix::none;
87             sngcpp::cppparser::ParseIntegerLiteral(lexer.FileName(), token, value, base, suffix);
88             return new sngcpp::ast::IntegerLiteralNode(span, value, suffix, base, token.match.ToString());
89         }
90         ;
91 }