]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/rust/parse/rust-cfg-parser.cc
1 /* This file is part of GCC.
3 GCC is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 3, or (at your option)
8 GCC is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with GCC; see the file COPYING3. If not see
15 <http://www.gnu.org/licenses/>. */
17 #include "rust-cfg-parser.h"
19 #include "rust-parse.h"
20 #include "rust-session-manager.h"
25 parse_cfg_option (std::string
&input
, std::string
&key
, std::string
&value
)
30 auto lexer
= Lexer (input
);
31 auto parser
= Parser
<Lexer
> (lexer
);
33 auto token
= parser
.peek_current_token ();
34 if (token
->get_id () != IDENTIFIER
)
39 key
= token
->get_str ();
41 rust_assert (parser
.skip_token (IDENTIFIER
));
42 token
= parser
.peek_current_token ();
44 switch (token
->get_id ())
47 // we're done parsing, we had a valid key, return happily
50 // We have an equal sign: Skip the token and parse an identifier
52 rust_assert (parser
.skip_token (EQUAL
));
54 auto value_expr
= parser
.parse_literal_expr ();
55 // We had an equal sign but no value, error out
59 if (value_expr
->get_lit_type () != AST::Literal::LitType::STRING
)
62 value
= value_expr
->get_literal ().as_string ();
76 rust_cfg_parser_test (void)
81 auto input
= std::string ("key_no_value");
83 ASSERT_TRUE (Rust::parse_cfg_option (input
, key
, value
));
84 ASSERT_EQ (key
, "key_no_value");
85 ASSERT_TRUE (value
.empty ());
87 input
= std::string ("k=\"v\"");
89 ASSERT_TRUE (Rust::parse_cfg_option (input
, key
, value
));
91 ASSERT_EQ (value
, "v");
93 // values should be between double quotes
94 input
= std::string ("k=v");
95 ASSERT_FALSE (Rust::parse_cfg_option (input
, key
, value
));
97 // No value is an error if there is an equal sign
98 input
= std::string ("k=");
99 ASSERT_FALSE (Rust::parse_cfg_option (input
, key
, value
));
101 // No key is an error
102 input
= std::string ("=");
103 ASSERT_FALSE (Rust::parse_cfg_option (input
, key
, value
));
105 input
= std::string ("=value");
106 ASSERT_FALSE (Rust::parse_cfg_option (input
, key
, value
));
108 // values that are not string literals are an error
109 input
= std::string ("key=b\"a\"");
110 ASSERT_FALSE (Rust::parse_cfg_option (input
, key
, value
));
112 input
= std::string ("key='v'");
113 ASSERT_FALSE (Rust::parse_cfg_option (input
, key
, value
));
115 input
= std::string ("key=155");
116 ASSERT_FALSE (Rust::parse_cfg_option (input
, key
, value
));
118 input
= std::string ("key=3.14");
119 ASSERT_FALSE (Rust::parse_cfg_option (input
, key
, value
));
121 // kebab case is not valid for an identifier
122 input
= std::string ("key-no-value");
123 ASSERT_FALSE (Rust::parse_cfg_option (input
, key
, value
));
125 } // namespace selftest