Skip to content

Commit dc990ec

Browse files
committed
Merge pull request #171 from sparkprime/fix_keywords
Fix #170
2 parents 96560a8 + a161de1 commit dc990ec

7 files changed

+56
-43
lines changed

core/formatter.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616

1717
#include <typeinfo>
1818

19+
#include "lexer.h"
1920
#include "formatter.h"
2021
#include "string_utils.h"
2122
#include "unicode.h"
@@ -1281,6 +1282,9 @@ class PrettyFieldNames : public Pass {
12811282
continue;
12821283
return false;
12831284
}
1285+
// Filter out keywords.
1286+
if (lex_get_keyword_kind(encode_utf8(str)) != Token::IDENTIFIER)
1287+
return false;
12841288
return true;
12851289
}
12861290

@@ -1964,12 +1968,12 @@ std::string jsonnet_fmt(AST *ast, Fodder &final_fodder, const FmtOpts &opts)
19641968
StripAllButComments(alloc, opts).file(ast, final_fodder);
19651969
else if (opts.stripEverything)
19661970
StripEverything(alloc, opts).file(ast, final_fodder);
1971+
if (opts.prettyFieldNames)
1972+
PrettyFieldNames(alloc, opts).file(ast, final_fodder);
19671973
if (opts.stringStyle != 'l')
19681974
EnforceStringStyle(alloc, opts).file(ast, final_fodder);
19691975
if (opts.commentStyle != 'l')
19701976
EnforceCommentStyle(alloc, opts).file(ast, final_fodder);
1971-
if (opts.prettyFieldNames)
1972-
PrettyFieldNames(alloc, opts).file(ast, final_fodder);
19731977
if (opts.indent > 0)
19741978
FixIndentation(alloc, opts).file(ast, final_fodder);
19751979

core/lexer.cpp

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616

1717
#include <cassert>
1818

19+
#include <map>
1920
#include <string>
2021
#include <sstream>
2122

@@ -153,6 +154,33 @@ static bool is_symbol(char c)
153154
return false;
154155
}
155156

157+
static const std::map<std::string, Token::Kind> keywords = {
158+
{"assert", Token::ASSERT},
159+
{"else", Token::ELSE},
160+
{"error", Token::ERROR},
161+
{"false", Token::FALSE},
162+
{"for", Token::FOR},
163+
{"function", Token::FUNCTION},
164+
{"if", Token::IF},
165+
{"import", Token::IMPORT},
166+
{"importstr", Token::IMPORTSTR},
167+
{"in", Token::IN},
168+
{"local", Token::LOCAL},
169+
{"null", Token::NULL_LIT},
170+
{"self", Token::SELF},
171+
{"super", Token::SUPER},
172+
{"tailstrict", Token::TAILSTRICT},
173+
{"then", Token::THEN},
174+
{"true", Token::TRUE},
175+
};
176+
177+
Token::Kind lex_get_keyword_kind(const std::string &identifier)
178+
{
179+
auto it = keywords.find(identifier);
180+
if (it == keywords.end()) return Token::IDENTIFIER;
181+
return it->second;
182+
}
183+
156184
std::string lex_number(const char *&c, const std::string &filename, const Location &begin)
157185
{
158186
// This function should be understood with reference to the linked image:
@@ -485,44 +513,7 @@ Tokens jsonnet_lex(const std::string &filename, const char *input)
485513
std::string id;
486514
for (; is_identifier(*c); ++c)
487515
id += *c;
488-
if (id == "assert") {
489-
kind = Token::ASSERT;
490-
} else if (id == "else") {
491-
kind = Token::ELSE;
492-
} else if (id == "error") {
493-
kind = Token::ERROR;
494-
} else if (id == "false") {
495-
kind = Token::FALSE;
496-
} else if (id == "for") {
497-
kind = Token::FOR;
498-
} else if (id == "function") {
499-
kind = Token::FUNCTION;
500-
} else if (id == "if") {
501-
kind = Token::IF;
502-
} else if (id == "import") {
503-
kind = Token::IMPORT;
504-
} else if (id == "importstr") {
505-
kind = Token::IMPORTSTR;
506-
} else if (id == "in") {
507-
kind = Token::IN;
508-
} else if (id == "local") {
509-
kind = Token::LOCAL;
510-
} else if (id == "null") {
511-
kind = Token::NULL_LIT;
512-
} else if (id == "self") {
513-
kind = Token::SELF;
514-
} else if (id == "super") {
515-
kind = Token::SUPER;
516-
} else if (id == "tailstrict") {
517-
kind = Token::TAILSTRICT;
518-
} else if (id == "then") {
519-
kind = Token::THEN;
520-
} else if (id == "true") {
521-
kind = Token::TRUE;
522-
} else {
523-
// Not a keyword, must be an identifier.
524-
kind = Token::IDENTIFIER;
525-
}
516+
kind = lex_get_keyword_kind(id);
526517
data = id;
527518

528519
} else if (is_symbol(*c) || *c == '#') {

core/lexer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ static inline std::ostream &operator<<(std::ostream &o, const Token &v)
236236
return o;
237237
}
238238

239+
/** IF the given identifier is a keyword, return its kind, otherwise return IDENTIFIER. */
240+
Token::Kind lex_get_keyword_kind(const std::string &identifier);
241+
239242
Tokens jsonnet_lex(const std::string &filename, const char *input);
240243

241244
std::string jsonnet_unlex(const Tokens &tokens);

test_suite/formatter.jsonnet

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,9 @@ limitations under the License.
269269
test_field19:: [1, 2, 3][/*foo*/ 1/*bar*/:],
270270
test_field20:: [1, 2, 3][/*foo*/ 1/*bar*/:/*baz*/:],
271271

272+
prettyFields: {
273+
'identifier': true,
274+
"not identifier": true,
275+
"function": true, // Test keyword.
276+
}
272277
}

test_suite/formatter.jsonnet.fmt.golden

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,9 @@ limitations under the License.
269269
test_field19:: [1, 2, 3][/*foo*/ 1/*bar*/:],
270270
test_field20:: [1, 2, 3][/*foo*/ 1/*bar*/:/*baz*/],
271271

272+
prettyFields: {
273+
identifier: true,
274+
"not identifier": true,
275+
"function": true, // Test keyword.
276+
},
272277
}

test_suite/formatter.jsonnet.golden

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
{
22
"func": null,
3+
"prettyFields": {
4+
"function": true,
5+
"identifier": true,
6+
"not identifier": true
7+
},
38
"test_field0A": {
49
"g": 1
510
},

test_suite/unparse.jsonnet.fmt.golden

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ limitations under the License.
2424
string2: '"foo\n bar\n\n\'bar\u0005\"\'\t \u0050\b\f\r\\',
2525
lit_field1: 1,
2626
lit_field2: 1,
27-
false: false,
28-
true: true,
29-
null: null,
27+
"false": false,
28+
"true": true,
29+
"null": null,
3030
hidden_field:: null,
3131
hidden_field2:: null,
3232
}

0 commit comments

Comments
 (0)