Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_parse_block_without_closing_curly_brace():
stream = (
tokens.LiteralToken("view", 1),
tokens.ValueToken(1),
tokens.BlockStartToken(1),
tokens.LiteralToken("hidden", 2),
tokens.ValueToken(2),
tokens.LiteralToken("yes", 2),
tokens.StreamEndToken(3),
)
parser = lkml.parser.Parser(stream)
result = parser.parse_block()
assert result is None
def parser():
stream = (
tokens.StreamStartToken(1),
tokens.LiteralToken("view", 1),
tokens.ValueToken(1),
tokens.LiteralToken("view_name", 1),
tokens.BlockStartToken(1),
tokens.LiteralToken("sql_table_name", 2),
tokens.ValueToken(2),
tokens.ExpressionBlockToken("schema.table_name", 2),
tokens.ExpressionBlockEndToken(2),
tokens.LiteralToken("drill_fields", 3),
tokens.ValueToken(3),
tokens.ListStartToken(3),
tokens.LiteralToken("view_name.field_one", 3),
tokens.CommaToken(3),
tokens.LiteralToken("view_name.field_two", 3),
tokens.ListEndToken(3),
tokens.BlockEndToken(4),
tokens.StreamEndToken(4),
)
return lkml.parser.Parser(stream)
lexer.scan_until_token()
result = lexer.peek()
assert result == "S"
def test_scan_until_token_skips_comments():
text = "# This is a comment\nStart here"
lexer = lkml.Lexer(text)
lexer.scan_until_token()
result = lexer.peek()
assert result == "S"
params = [
("\0", tokens.StreamEndToken(1)),
("{", tokens.BlockStartToken(1)),
("}", tokens.BlockEndToken(1)),
("[", tokens.ListStartToken(1)),
("]", tokens.ListEndToken(1)),
(",", tokens.CommaToken(1)),
(":", tokens.ValueToken(1)),
(";;", tokens.ExpressionBlockEndToken(1)),
]
@pytest.mark.parametrize("text,expected", params)
def test_scan_all_simple_tokens(text, expected):
lexer = lkml.Lexer(text)
result = lexer.scan()
# Skip stream start token appended at the beginning
assert result[1] == expected
def test_parse_block_with_no_expression():
stream = (
tokens.LiteralToken("dimension", 1),
tokens.ValueToken(1),
tokens.LiteralToken("dimension_name", 1),
tokens.BlockStartToken(1),
tokens.BlockEndToken(1),
tokens.StreamEndToken(1),
)
parser = lkml.parser.Parser(stream)
result = parser.parse_block()
assert result == {"dimension": {"name": "dimension_name"}}
"""
if self.log_debug:
grammar = "[block] = key literal? '{' expression '}'"
self.logger.debug("%sTry to parse %s", self.depth * DELIMITER, grammar)
key = self.parse_key()
if key is None:
return key
if self.check(tokens.LiteralToken):
literal = self.consume_token_value()
else:
literal = None
if self.check(tokens.BlockStartToken):
self.advance()
else:
return None
expression = self.parse_expression()
if self.check(tokens.BlockEndToken):
self.advance()
block = {key: expression}
if literal:
block[key]["name"] = literal
if self.log_debug:
self.logger.debug(
"%sSuccessfully parsed block.", self.depth * DELIMITER
)
# These are keys for fields in Looker that have a "name" attribute. Since lkml uses the
# key `name` to represent the name of the field (e.g. for `dimension: dimension_name {`,
# the `name` key would hold the value `dimension_name`.)
KEYS_WITH_NAME_FIELDS: Tuple[str, ...] = (
"user_attribute_param",
"param",
"form_param",
"option",
)
CHARACTER_TO_TOKEN: Dict[str, Type[tokens.Token]] = {
"\0": tokens.StreamEndToken,
"{": tokens.BlockStartToken,
"}": tokens.BlockEndToken,
"[": tokens.ListStartToken,
"]": tokens.ListEndToken,
",": tokens.CommaToken,
":": tokens.ValueToken,
";": tokens.ExpressionBlockEndToken,
}