diff --git a/CHANGELOG.md b/CHANGELOG.md index 74e2aa4..dfb68c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,15 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) -The Working in Progress (WIP) section is for changes that are already in master, but haven't been published to Godot's asset library yet. Even though the section is called WIP, all changes in master are stable and functional. +## 1.0.2 (2021-06-04) + +### Added + +- Condition blocks before line can use the "when" keyword. + +### Fixed + +- Diverts support conditional blocks before and after line ## 1.0.1 (2021-05-09) diff --git a/addons/clyde/parser/Lexer.gd b/addons/clyde/parser/Lexer.gd index 5a519b1..abef9a2 100644 --- a/addons/clyde/parser/Lexer.gd +++ b/addons/clyde/parser/Lexer.gd @@ -421,7 +421,13 @@ func _handle_divert(): _position += 1 _column += 1 - return Token(TOKEN_DIVERT, _line, initial_column, _array_join(values).strip_edges()) + var token = Token(TOKEN_DIVERT, _line, initial_column, _array_join(values).strip_edges()) + + var linebreak = _get_following_line_break() + if linebreak: + return [ token, linebreak ] + + return token func _handle_divert_parent(): @@ -429,8 +435,13 @@ func _handle_divert_parent(): _position += 2 _column += 2 - return Token(TOKEN_DIVERT_PARENT, _line, initial_column) + var token = Token(TOKEN_DIVERT_PARENT, _line, initial_column) + var linebreak = _get_following_line_break() + if linebreak: + return [ token, linebreak ] + + return token func _handle_start_variations(): var initial_column = _column diff --git a/addons/clyde/parser/Parser.gd b/addons/clyde/parser/Parser.gd index 13148b8..f38122e 100644 --- a/addons/clyde/parser/Parser.gd +++ b/addons/clyde/parser/Parser.gd @@ -142,6 +142,8 @@ func _lines(): if _tokens.peek([Lexer.TOKEN_KEYWORD_SET, Lexer.TOKEN_KEYWORD_TRIGGER]): lines = [_line_with_action()] else: + if _tokens.peek([Lexer.TOKEN_KEYWORD_WHEN]): + _tokens.consume([Lexer.TOKEN_KEYWORD_WHEN]) lines = [_conditional_line()] @@ -358,11 +360,26 @@ func _divert(): _tokens.consume([ Lexer.TOKEN_DIVERT, Lexer.TOKEN_DIVERT_PARENT ]) var divert = _tokens.current_token + var token match divert.token: Lexer.TOKEN_DIVERT: - return DivertNode(divert.value) + token = DivertNode(divert.value) Lexer.TOKEN_DIVERT_PARENT: - return DivertNode('') + token = DivertNode('') + + if _tokens.peek([Lexer.TOKEN_LINE_BREAK]): + _tokens.consume([Lexer.TOKEN_LINE_BREAK]) + return token + + if _tokens.peek([Lexer.TOKEN_EOF]): + return token + + if _tokens.peek([Lexer.TOKEN_BRACE_OPEN]): + _tokens.consume([Lexer.TOKEN_BRACE_OPEN]) + token = _line_with_action(token) + + + return token func _variations(): diff --git a/addons/clyde/plugin.cfg b/addons/clyde/plugin.cfg index 68972a8..15a6bcb 100644 --- a/addons/clyde/plugin.cfg +++ b/addons/clyde/plugin.cfg @@ -3,5 +3,5 @@ name="Clyde Dialogue" description="Interpreter and importer for Clyde Dialogue Language" author="Vinicius Gerevini" -version="1.0.1" +version="1.0.2" script="plugin.gd" diff --git a/test/test_lexer.gd b/test/test_lexer.gd index a1e93ad..7fa086f 100644 --- a/test/test_lexer.gd +++ b/test/test_lexer.gd @@ -343,16 +343,38 @@ hello assert_eq_deep(tokens, [ { "token": Lexer.TOKEN_TEXT, "value": 'hello', "line": 1, "column": 0, }, { "token": Lexer.TOKEN_DIVERT, "value": 'first divert', "line": 2, "column": 0, }, + { "token": Lexer.TOKEN_LINE_BREAK, "value": null, "line": 2, "column": 15, }, { "token": Lexer.TOKEN_OPTION, "line": 4, "column": 0, "value": null }, { "token": Lexer.TOKEN_TEXT, "value": 'test', "line": 4, "column": 2 }, { "token": Lexer.TOKEN_INDENT, "line": 5, "column": 0, "value": null }, { "token": Lexer.TOKEN_DIVERT, "value": 'divert', "line": 5, "column": 2 }, + { "token": Lexer.TOKEN_LINE_BREAK, "value": null, "line": 5, "column": 11, }, { "token": Lexer.TOKEN_DIVERT_PARENT, "line": 6, "column": 2, "value": null }, + { "token": Lexer.TOKEN_LINE_BREAK, "value": null, "line": 6, "column": 4, }, { "token": Lexer.TOKEN_DIVERT, "value": 'END', "line": 7, "column": 2 }, + { "token": Lexer.TOKEN_LINE_BREAK, "value": null, "line": 7, "column": 8, }, { "token": Lexer.TOKEN_EOF, "line": 8, "column": 0, "value": null}, ]) +func test_divert_on_eof(): + var lexer = Lexer.new() + var tokens = lexer.init("-> div").get_all() + assert_eq_deep(tokens, [ + { "token": Lexer.TOKEN_DIVERT, "value": 'div', "line": 0, "column": 0, }, + { "token": Lexer.TOKEN_EOF, "line": 0, "column": 6, "value": null}, + ]) + + +func test_divert_parent_on_eof(): + var lexer = Lexer.new() + var tokens = lexer.init("<-").get_all() + assert_eq_deep(tokens, [ + { "token": Lexer.TOKEN_DIVERT_PARENT, "value": null, "line": 0, "column": 0, }, + { "token": Lexer.TOKEN_EOF, "line": 0, "column": 2, "value": null}, + ]) + + func test_variations(): var lexer = Lexer.new() var tokens = lexer.init(""" @@ -390,6 +412,7 @@ func test_variations(): { "token": Lexer.TOKEN_INDENT, "line": 7, "column": 0, "value": null }, { "token": Lexer.TOKEN_MINUS, "line": 7, "column": 2, "value": null }, { "token": Lexer.TOKEN_DIVERT, "value": 'nope', "line": 7, "column": 4 }, + { "token": Lexer.TOKEN_LINE_BREAK, "value": null, "line": 7, "column": 11, }, { "token": Lexer.TOKEN_MINUS, "line": 8, "column": 2, "value": null, }, { "token": Lexer.TOKEN_TEXT, "value": 'yep', "line": 8, "column": 4 }, { "token": Lexer.TOKEN_DEDENT, "line": 9, "column": 0, "value": null }, diff --git a/test/test_parser_logic.gd b/test/test_parser_logic.gd index 50563d8..f5c26d6 100644 --- a/test/test_parser_logic.gd +++ b/test/test_parser_logic.gd @@ -378,6 +378,17 @@ func test_string_literal(): ]) assert_eq_deep(result, expected) +func test_condition_before_line_with_keyword(): + var result = parse("{ when some_var } This is conditional") + var expected = _create_doc_payload([ + { + "type": "conditional_content", + "conditions": { "type": "variable", "name": "some_var" }, + "content": { "type": "line", "value": "This is conditional", "speaker": null, "id": null, "tags": null, } + }, + ]) + assert_eq_deep(result, expected) + func test_condition_after_line(): var result = parse("This is conditional { when some_var }") @@ -416,6 +427,18 @@ func test_conditional_divert(): assert_eq_deep(result, expected) +func test_conditional_divert_after(): + var result = parse("-> some_block { some_var }") + var expected = _create_doc_payload([ + { + "type": "conditional_content", + "conditions": { "type": "variable", "name": "some_var" }, + "content": { "type": "divert", "target": "some_block", } + }, + ]) + assert_eq_deep(result, expected) + + func test_conditional_option(): var result = parse(""" * { some_var } option 1 @@ -864,6 +887,25 @@ func test_options_assignment(): assert_eq_deep(result, expected) +func test_divert_with_assignment(): + var result = parse("-> go { set a = 2 }") + var expected = _create_doc_payload([{ + "type": 'action_content', + "action": { + "type": 'assignments', + "assignments": [ + { + "type": 'assignment', + "variable": { "type": 'variable', "name": 'a', }, + "operation": 'assign', + "value": { "type": 'literal', "name": 'number', "value": 2.0, }, + }, + ], + }, + "content": { "type": 'divert', "target": 'go' }, + }]) + assert_eq_deep(result, expected) + func test_trigger_event(): var result = parse("{ trigger some_event } trigger")