Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief This file contains a wrapper for Bison’s `symbol_type`.
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #ifndef ELEKTRA_PLUGIN_YAMBI_SYMBOL_HPP
10 : #define ELEKTRA_PLUGIN_YAMBI_SYMBOL_HPP
11 :
12 : // -- Imports ------------------------------------------------------------------
13 :
14 : #include "parser.hpp"
15 :
16 : typedef yambi::Parser Parser;
17 :
18 : typedef Parser::location_type location_type;
19 : typedef Parser::symbol_type symbol_type;
20 : typedef Parser::token_type token_type;
21 : typedef Parser::token token;
22 :
23 : // -- Macros -------------------------------------------------------------------
24 :
25 : #define ELEKTRA_SWITCH_TOKEN(TOK) \
26 : case token::TOK: \
27 : return Parser::make_##TOK (text, placement)
28 :
29 : // -- Class --------------------------------------------------------------------
30 :
31 : /**
32 : * This class acts as wrapper for `symbol_type`, which we can not use inside a
33 : * queue, since its copy assignment operator is private.
34 : */
35 11173 : class Symbol
36 : {
37 : /** This variable stores the location of this symbol. */
38 : location_type placement;
39 :
40 : /** This variable specifies the token type of this symbol. */
41 : token_type tokenType;
42 :
43 : /** This variable stores the actual value of the symbol. */
44 : std::string text;
45 :
46 : public:
47 : /**
48 : * @brief This constructor creates a symbol from the given arguments.
49 : *
50 : * @param type This argument specifies the token type of the symbol.
51 : * @param location This argument specifies the location of the symbol.
52 : * @param value This variable stores the value of this symbol.
53 : */
54 : Symbol (token_type const & type, location_type const & location, std::string const & value = "")
55 2249 : : placement{ location }, tokenType{ type }, text{ value }
56 : {
57 : }
58 :
59 : /**
60 : * @brief This method returns the Bison symbol represented by this object.
61 : *
62 : * @return A symbol representing this object
63 : */
64 1003 : symbol_type get () const
65 : {
66 1003 : switch (tokenType)
67 : {
68 63 : ELEKTRA_SWITCH_TOKEN (STREAM_START);
69 63 : ELEKTRA_SWITCH_TOKEN (STREAM_END);
70 24 : ELEKTRA_SWITCH_TOKEN (COMMENT);
71 212 : ELEKTRA_SWITCH_TOKEN (PLAIN_SCALAR);
72 2 : ELEKTRA_SWITCH_TOKEN (SINGLE_QUOTED_SCALAR);
73 65 : ELEKTRA_SWITCH_TOKEN (DOUBLE_QUOTED_SCALAR);
74 57 : ELEKTRA_SWITCH_TOKEN (MAP_START);
75 57 : ELEKTRA_SWITCH_TOKEN (MAP_END);
76 108 : ELEKTRA_SWITCH_TOKEN (KEY);
77 108 : ELEKTRA_SWITCH_TOKEN (VALUE);
78 42 : ELEKTRA_SWITCH_TOKEN (SEQUENCE_START);
79 42 : ELEKTRA_SWITCH_TOKEN (SEQUENCE_END);
80 97 : ELEKTRA_SWITCH_TOKEN (ELEMENT);
81 : default:
82 63 : return Parser::make_END (placement);
83 : }
84 : }
85 :
86 : /**
87 : * @brief This method returns the location of this symbol.
88 : *
89 : * @return The location of this symbol
90 : */
91 : yambi::location getLocation () const
92 : {
93 108 : return placement;
94 : }
95 :
96 : /**
97 : * @brief This method returns a string representation of this symbol.
98 : *
99 : * @return A string representing this symbol
100 : */
101 : std::string toString () const
102 : {
103 : return "<" + std::to_string (placement.begin.line) + ":" + std::to_string (placement.begin.column) + "," +
104 : std::to_string (placement.end.line) + ":" + std::to_string (placement.end.column) + "," + text + ">";
105 : }
106 : };
107 :
108 : #endif // ELEKTRA_PLUGIN_YAMBI_SYMBOL_HPP
|