Skip to content

Commit

Permalink
Merge pull request #105 from yui-knk/remove_code_braces
Browse files Browse the repository at this point in the history
Remove surrounding braces from user code
  • Loading branch information
yui-knk committed Oct 6, 2023
2 parents 26c8b37 + c4b4ae3 commit 884c91c
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 125 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
# Copy from https://github.com/ruby/ruby/blob/089227e94823542acfdafa68541d330eee42ffea/.github/workflows/check_misc.yml#L27
- name: Check for trailing spaces
run: |
git grep -I -n '[ ]$' -- '*.rb' '*.[chy]' '*.rs' && exit 1 || :
git grep -I -n '[ ]$' -- '*.rb' '*.[chy]' '*.rs' ':!spec/' && exit 1 || :
git grep -n '^[ ][ ]*$' -- '*.md' && exit 1 || :
steep-check:
runs-on: ubuntu-20.04
Expand Down
4 changes: 2 additions & 2 deletions lib/lrama/grammar/union.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module Lrama
class Grammar
class Union < Struct.new(:code, :lineno, keyword_init: true)
def braces_less_code
# Remove braces
code.s_value[1..-2]
# Braces is already removed by lexer
code.s_value
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/lrama/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def lex_user_code(ss, line, column, lines)
first_column = column
debug("Enter lex_user_code: #{line}")
brace_count = 1
str = "{"
str = ""
# Array of [type, $n, tag, first column, last column]
# TODO: Is it better to keep string, like "$$", and use gsub?
references = []
Expand Down Expand Up @@ -247,7 +247,6 @@ def lex_user_code(ss, line, column, lines)

debug("Return lex_user_code: #{line}")
if brace_count == 0
str << ss[0]
user_code = Token.new(type: Token::User_code, s_value: str.freeze)
user_code.line = first_line
user_code.column = first_column
Expand Down
16 changes: 8 additions & 8 deletions lib/lrama/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def symbol_actions_for_printer
str << <<-STR
case #{sym.enum_name}: /* #{sym.comment} */
#line #{sym.printer.lineno} "#{@grammar_file_path}"
#{sym.printer.translated_code(sym.tag)}
{#{sym.printer.translated_code(sym.tag)}}
#line [@oline@] [@ofile@]
break;
Expand All @@ -160,7 +160,7 @@ def user_initial_action(comment = "")
<<-STR
#{comment}
#line #{@grammar.initial_action.line} "#{@grammar_file_path}"
#{@grammar.initial_action.translated_code}
{#{@grammar.initial_action.translated_code}}
STR
end

Expand All @@ -173,7 +173,7 @@ def symbol_actions_for_error_token
str << <<-STR
case #{sym.enum_name}: /* #{sym.comment} */
#line #{sym.error_token.lineno} "#{@grammar_file_path}"
#{sym.error_token.translated_code(sym.tag)}
{#{sym.error_token.translated_code(sym.tag)}}
#line [@oline@] [@ofile@]
break;
Expand All @@ -197,7 +197,7 @@ def user_actions
str << <<-STR
case #{rule.id + 1}: /* #{rule.as_comment} */
#line #{code.line} "#{@grammar_file_path}"
#{spaces}#{rule.translated_code}
#{spaces}{#{rule.translated_code}}
#line [@oline@] [@ofile@]
break;
Expand All @@ -212,22 +212,22 @@ def user_actions
str
end

def omit_braces_and_blanks(param)
param[1..-2].strip
def omit_blanks(param)
param.strip
end

# b4_parse_param
def parse_param
if @grammar.parse_param
omit_braces_and_blanks(@grammar.parse_param)
omit_blanks(@grammar.parse_param)
else
""
end
end

def lex_param
if @grammar.lex_param
omit_braces_and_blanks(@grammar.lex_param)
omit_blanks(@grammar.lex_param)
else
""
end
Expand Down
70 changes: 35 additions & 35 deletions spec/lrama/lexer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,26 +110,26 @@ class : keyword_class tSTRING keyword_end %prec tPLUS
T.new(type: T::Ident, s_value: "verbose"),

T.new(type: T::P_printer, s_value: "%printer"),
T.new(type: T::User_code, s_value: "{\n print_int();\n}"),
T.new(type: T::User_code, s_value: "\n print_int();\n"),
T.new(type: T::Tag, s_value: "<int>"),

T.new(type: T::P_printer, s_value: "%printer"),
T.new(type: T::User_code, s_value: "{\n print_token();\n}"),
T.new(type: T::User_code, s_value: "\n print_token();\n"),
T.new(type: T::Ident, s_value: "tNUMBER"),
T.new(type: T::Ident, s_value: "tSTRING"),

T.new(type: T::P_lex_param, s_value: "%lex-param"),
T.new(type: T::User_code, s_value: "{struct lex_params *p}"),
T.new(type: T::User_code, s_value: "struct lex_params *p"),

T.new(type: T::P_parse_param, s_value: "%parse-param"),
T.new(type: T::User_code, s_value: "{struct parse_params *p}"),
T.new(type: T::User_code, s_value: "struct parse_params *p"),

T.new(type: T::P_initial_action, s_value: "%initial-action"),
T.new(type: T::User_code, s_value: "{\n initial_action_func(@$);\n}"),
T.new(type: T::User_code, s_value: "\n initial_action_func(@$);\n"),
T.new(type: T::Semicolon, s_value: ";"),

T.new(type: T::P_union, s_value: "%union"),
T.new(type: T::User_code, s_value: "{\n int i;\n long l;\n char *str;\n}"),
T.new(type: T::User_code, s_value: "\n int i;\n long l;\n char *str;\n"),

T.new(type: T::P_token, s_value: "%token"),
T.new(type: T::Ident, s_value: "EOI"),
Expand Down Expand Up @@ -218,25 +218,25 @@ class : keyword_class tSTRING keyword_end %prec tPLUS
T.new(type: T::Ident, s_value: "keyword_end"),
T.new(type: T::P_prec, s_value: "%prec"),
T.new(type: T::Ident, s_value: "tPLUS"),
T.new(type: T::User_code, s_value: "{ code 1 }"),
T.new(type: T::User_code, s_value: " code 1 "),

T.new(type: T::Bar, s_value: "|"),
T.new(type: T::Ident, s_value: "keyword_class"),
T.new(type: T::User_code, s_value: "{ code 2 }"),
T.new(type: T::User_code, s_value: " code 2 "),
T.new(type: T::Ident, s_value: "tSTRING"),
T.new(type: T::Char, s_value: "'!'"),
T.new(type: T::Ident, s_value: "keyword_end"),
T.new(type: T::User_code, s_value: "{ code 3 }"),
T.new(type: T::User_code, s_value: " code 3 "),
T.new(type: T::P_prec, s_value: "%prec"),
T.new(type: T::String, s_value: "\"=\""),

T.new(type: T::Bar, s_value: "|"),
T.new(type: T::Ident, s_value: "keyword_class"),
T.new(type: T::User_code, s_value: "{ code 4 }"),
T.new(type: T::User_code, s_value: " code 4 "),
T.new(type: T::Ident, s_value: "tSTRING"),
T.new(type: T::Char, s_value: "'?'"),
T.new(type: T::Ident, s_value: "keyword_end"),
T.new(type: T::User_code, s_value: "{ code 5 }"),
T.new(type: T::User_code, s_value: " code 5 "),
T.new(type: T::P_prec, s_value: "%prec"),
T.new(type: T::Char, s_value: "'>'"),
T.new(type: T::Semicolon, s_value: ";"),
Expand Down Expand Up @@ -342,29 +342,29 @@ class : keyword_class tSTRING keyword_end %prec tPLUS

expect(user_codes.map(&:references)).to eq([
[
[:dollar, 1, T.new(type: T::Tag, s_value: "<int>"), 2, 8],
[:dollar, "$", T.new(type: T::Tag, s_value: "<int>"), 15, 21]
[:dollar, 1, T.new(type: T::Tag, s_value: "<int>"), 1, 7],
[:dollar, "$", T.new(type: T::Tag, s_value: "<int>"), 14, 20]
],
[
[:dollar, "$", T.new(type: T::Tag, s_value: "<int>"), 2, 8]
[:dollar, "$", T.new(type: T::Tag, s_value: "<int>"), 1, 7]
],
[
[:dollar, "$", T.new(type: T::Tag, s_value: "<int>"), 2, 8]
[:dollar, "$", T.new(type: T::Tag, s_value: "<int>"), 1, 7]
],
[],
[
[:dollar, 2, nil, 2, 3],
[:dollar, 3, nil, 6, 7],
[:dollar, 5, nil, 10, 11],
[:dollar, 7, nil, 14, 15],
[:dollar, 10, nil, 18, 20],
[:dollar, "$", nil, 23, 24],
[:at, 2, nil, 43, 44],
[:at, 3, nil, 47, 48],
[:at, 5, nil, 51, 52],
[:at, 7, nil, 55, 56],
[:at, 10, nil, 59, 61],
[:at, "$", nil, 64, 65],
[:dollar, 2, nil, 1, 2],
[:dollar, 3, nil, 5, 6],
[:dollar, 5, nil, 9, 10],
[:dollar, 7, nil, 13, 14],
[:dollar, 10, nil, 17, 19],
[:dollar, "$", nil, 22, 23],
[:at, 2, nil, 42, 43],
[:at, 3, nil, 46, 47],
[:at, 5, nil, 50, 51],
[:at, 7, nil, 54, 55],
[:at, 10, nil, 58, 60],
[:at, "$", nil, 63, 64],
],
])
end
Expand Down Expand Up @@ -394,7 +394,7 @@ class : keyword_class tSTRING keyword_end %prec tPLUS
expect(lexer.grammar_rules_tokens).to eq([
T.new(type: T::Ident_Colon, s_value: "line"),
T.new(type: T::Ident, s_value: "expr"),
T.new(type: T::User_code, s_value: %Q({ printf("\t%.10g\n", $expr); })),
T.new(type: T::User_code, s_value: %Q( printf("\t%.10g\n", $expr); )),
T.new(type: T::Semicolon, s_value: ";"),

T.new(type: T::Ident_Colon, s_value: "expr"),
Expand All @@ -408,7 +408,7 @@ class : keyword_class tSTRING keyword_end %prec tPLUS
T.new(type: T::Ident, s_value: "expr"),
T.new(type: T::Named_Ref, s_value: "right"),
T.new(type: T::Char, s_value: "'+'"),
T.new(type: T::User_code, s_value: "{ $result = $left + $right; }"),
T.new(type: T::User_code, s_value: " $result = $left + $right; "),
T.new(type: T::Semicolon, s_value: ";"),
])

Expand All @@ -418,12 +418,12 @@ class : keyword_class tSTRING keyword_end %prec tPLUS

expect(user_codes.map(&:references)).to eq([
[
[:dollar, "expr", nil, 20, 24],
[:dollar, "expr", nil, 19, 23],
],
[
[:dollar, "result", nil, 2, 8],
[:dollar, "left", nil, 12, 16],
[:dollar, "right", nil, 20, 25],
[:dollar, "result", nil, 1, 7],
[:dollar, "left", nil, 11, 15],
[:dollar, "right", nil, 19, 24],
]
])
end
Expand Down Expand Up @@ -463,13 +463,13 @@ class : keyword_class tSTRING keyword_end %prec tPLUS
end

expected = <<-CODE.chomp
{
int i = 1; /* @ */ // @
int j = 1; /* $ */ // $
int k = 1; /* @1 */ // @1
int l = 1; /* $$ */ // $$
int m = 1; /* $2 */ // $2
}
CODE

expect(user_codes.map(&:s_value)).to eq([expected])
Expand Down
Loading

0 comments on commit 884c91c

Please sign in to comment.