Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow variable refs in interpolated strings #36

Merged
merged 2 commits into from
Nov 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/piper/command/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ defmodule Piper.Command.Parser do
{:error, reason}
end
catch
error -> SemanticError.format_error(error)
error ->
SemanticError.format_error(error)
after
Process.delete(:piper_cp_context)
ParseContext.stop(context)
Expand Down
3 changes: 3 additions & 0 deletions src/piper_cmd2_interp_lexer.xrl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ Definitions.

INTERP_VALUE = \${([^\${}]|\\\$|\\{|\\})+}
TEXT = (\\\^.|\\.|[^\$])+
VAR_REF = \$([a-zA-Z0-9_\[\]\.]+)

Rules.

{VAR_REF} : {token, {text, ?advance_count(TokenChars), TokenChars}}.
{INTERP_VALUE} : {token, {interp_value, ?advance_count(TokenChars), expr_text(TokenChars)}}.
{TEXT} : {token, {text, ?advance_count(TokenChars), TokenChars}}.

Expand Down
35 changes: 34 additions & 1 deletion src/piper_cmd2_interp_parser.yrl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interpexpr parts.
Rootsymbol interpexpr.

interpexpr ->
parts : ?new_ast(interp_string, ['$1']).
parts : new_interpexpr('$1').

parts ->
text : [?new_ast(string, [text_to_string('$1')])].
Expand All @@ -26,3 +26,36 @@ Erlang code.

text_to_string({text, Position, Value}) ->
{string, Position, Value}.

new_interpexpr(Values) ->
case are_all_strings(Values) of
true ->
concatenate_strings(Values);
false ->
?new_ast(interp_string, [Values])
end.

are_all_strings([]) -> true;
are_all_strings([Str|T]) ->
case is_string(Str) of
true ->
are_all_strings(T);
false ->
false
end.

concatenate_strings([Str]) -> Str;
concatenate_strings([H|_]=Strings) ->
Values = [maps:get('value', Str) || Str <- Strings],
Updated = 'Elixir.Enum':join(Values),
maps:put('value', Updated, H).

is_string(Str) when is_map(Str) ->
case maps:get('__struct__', Str) of
'Elixir.Piper.Command.Ast.String' ->
true;
_ ->
false
end;
is_string(_) -> false.

11 changes: 2 additions & 9 deletions src/piper_cmd2_parser.yrl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Erlang code.
-export([parse_pipeline/1]).

parse_pipeline(Text) when is_binary(Text) ->
parse_pipeline(elixir_string_to_list(Text));
parse_pipeline(?parser_util:elixir_string_to_list(Text));
parse_pipeline(Text) ->
case piper_cmd2_lexer:scan(Text) of
{ok, Tokens, _} ->
Expand All @@ -84,7 +84,7 @@ parse_pipeline(Text) ->
end.

format_reason({error, {_, _, Reason}}) when is_list(Reason) ->
{error, list_to_elixir_string(Reason)};
{error, ?parser_util:list_to_elixir_string(Reason)};
format_reason({error, {_, _, Reason}}) ->
format_reason(Reason);
format_reason(Reason) when is_map(Reason) ->
Expand Down Expand Up @@ -141,10 +141,3 @@ ensure_quote_type_set(Value, QuoteType) ->
maps:put(quote_type, QuoteType, Value)
end.

elixir_string_to_list(Text) when is_binary(Text) ->
'Elixir.String':to_charlist(Text).

list_to_elixir_string(Text) when is_list(Text) ->
%% Ensure we're dealing with a flat list first
Text1 = lists:flatten(Text),
'Elixir.String.Chars':to_string(Text1).
13 changes: 12 additions & 1 deletion src/piper_cmd2_parser_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

-export([parse_token/3,
parse_token/4,
new_ast/2]).
new_ast/2,
elixir_string_to_list/1,
list_to_elixir_string/1]).

parse_token(Token, Name, Handler) ->
parse_token(Token, Name, Name, Handler).
Expand All @@ -29,6 +31,15 @@ parse_token({_, {Line, _} = Position, Value}, Lexer, Parser, Handler) ->
'Elixir.Piper.Command.ParseContext':set_position(Context, Old)
end.

elixir_string_to_list(Text) when is_binary(Text) ->
'Elixir.String':to_charlist(Text).

list_to_elixir_string(Text) when is_list(Text) ->
%% Ensure we're dealing with a flat list first
Text1 = lists:flatten(Text),
'Elixir.String.Chars':to_string(Text1).


new_ast(Name, Args) ->
apply(ast_node_for_name(Name), new, Args).

Expand Down
8 changes: 4 additions & 4 deletions src/piper_cmd2_var_parser.yrl
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ index_value({integer, _, Value}) ->
list_to_integer(Value).

key_value({text, _, [$'|_]=Value}) ->
re:replace(Value, "^'|'$", "", [{return, binary}, global]);
re:replace(Value, "^'|'$", "", [{return, binary}, global, unicode]);
key_value({text, _, [$"|_]=Value}) ->
re:replace(Value, "^\"|\"$", "", [{return, binary}, global]);
re:replace(Value, "^\"|\"$", "", [{return, binary}, global, unicode]);
key_value({text, _, Value}) ->
list_to_binary(Value).
?parser_util:list_to_elixir_string(Value).

make_variable(Var) ->
make_variable(Var, []).

make_variable({variable, Position, Name}, Ops) ->
Var1 = {variable, Position, re:replace(Name, "^\\$", "", [{return, list}, global])},
Var1 = {variable, Position, re:replace(Name, "^\\$", "", [{return, list}, global, unicode])},
?new_ast(variable, [Var1, Ops]).
42 changes: 42 additions & 0 deletions test/command/parser/parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -405,4 +405,46 @@ defmodule Parser.ParserTest do
assert "#{ast}" == "cfn:stack-purge-delete --region=us-west-1"
end

test "quoted strings containing var refs are parsed" do
{:ok, ast} = Parser.scan_and_parse("alias create \"echo $body[0]\"")
stage = ast.stages.left
assert stage.name.entity.value == "alias"
assert stage.name.bundle == nil
[first_arg, second_arg] = stage.args
assert first_arg.__struct__ == Piper.Command.Ast.String
assert "#{first_arg}" == "create"
assert second_arg.__struct__ == Piper.Command.Ast.String
assert "#{second_arg}" == "\"echo $body[0]\""
end

test "interpolated variable is parsed" do
{:ok, ast} = Parser.scan_and_parse("echo \"echo ${body[0]}\"")
stage = ast.stages.left
assert stage.name.entity.value == "echo"
[arg] = stage.args
assert arg.__struct__ == Piper.Command.Ast.InterpolatedString
[first_expr, second_expr] = arg.exprs
assert first_expr.__struct__ == Piper.Command.Ast.String
assert "#{first_expr}" == "echo "
assert second_expr.__struct__ == Piper.Command.Ast.Variable
assert Enum.count(second_expr.ops) == 1
assert "#{second_expr}" == "$body[0]"
assert "#{ast}" == "echo \"echo ${body[0]}\""
end

test "interpolated variable w/name deref is parsed" do
{:ok, ast} = Parser.scan_and_parse("echo \"echo The current region is ${region.name}\"")
stage = ast.stages.left
assert stage.name.entity.value == "echo"
[arg] = stage.args
assert arg.__struct__ == Piper.Command.Ast.InterpolatedString
[first_expr, second_expr] = arg.exprs
assert first_expr.__struct__ == Piper.Command.Ast.String
assert "#{first_expr}" == "echo The current region is "
assert second_expr.__struct__ == Piper.Command.Ast.Variable
assert Enum.count(second_expr.ops) == 1
assert "#{second_expr}" == "$region.name"
assert "#{ast}" == "echo \"echo The current region is ${region.name}\""
end

end