Skip to content

Commit

Permalink
Fixed @@Inject
Browse files Browse the repository at this point in the history
 * @@Inject test cases
 * some parser improvements
 * fixed handling aliases in @@Inject
  • Loading branch information
flyx committed Jan 26, 2018
1 parent 4d9b745 commit b1fa9c9
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 91 deletions.
1 change: 1 addition & 0 deletions annotation-test-suite/0010/===
@@ -0,0 +1 @@
Simple @@inject
2 changes: 2 additions & 0 deletions annotation-test-suite/0010/in.yaml
@@ -0,0 +1,2 @@
- foo
- @@inject [bar]
8 changes: 8 additions & 0 deletions annotation-test-suite/0010/processed.event
@@ -0,0 +1,8 @@
+STR
+DOC
+SEQ
=VAL :foo
=VAL :bar
-SEQ
-DOC
-STR
1 change: 1 addition & 0 deletions annotation-test-suite/0011/===
@@ -0,0 +1 @@
@@inject with empty list
3 changes: 3 additions & 0 deletions annotation-test-suite/0011/in.yaml
@@ -0,0 +1,3 @@
- foo
- bar
- @@inject []
8 changes: 8 additions & 0 deletions annotation-test-suite/0011/processed.event
@@ -0,0 +1,8 @@
+STR
+DOC
+SEQ
=VAL :foo
=VAL :bar
-SEQ
-DOC
-STR
1 change: 1 addition & 0 deletions annotation-test-suite/0012/===
@@ -0,0 +1 @@
@@inject with alias
2 changes: 2 additions & 0 deletions annotation-test-suite/0012/in.yaml
@@ -0,0 +1,2 @@
- &a [foo, bar]
- @@inject *a
12 changes: 12 additions & 0 deletions annotation-test-suite/0012/processed.event
@@ -0,0 +1,12 @@
+STR
+DOC
+SEQ
+SEQ &a
=VAL :foo
=VAL :bar
-SEQ
=VAL :foo
=VAL :bar
-SEQ
-DOC
-STR
Expand Up @@ -13,12 +13,10 @@ package body Yaml.Transformator.Annotation.Inject is
overriding function Next (Object : in out Instance) return Event is
begin
return Ret : constant Event := Object.Current do
if Object.State = Injecting'Access then
Object.Current_Exists := False;
elsif Object.State = Injecting_Aliased'Access then
if Object.State = Injecting_Aliased'Access then
Object.Injecting (Object.Current_Aliased.Value.Next);
else
raise Constraint_Error with "no event available";
Object.Current_Exists := False;
end if;
end return;
end Next;
Expand Down Expand Up @@ -147,6 +145,7 @@ package body Yaml.Transformator.Annotation.Inject is
end case;
if Object.Depth = 0 then
Object.State := After_Inject_End'Access;
Object.Current_Exists := False;
else
Object.Current_Exists := True;
end if;
Expand Down
137 changes: 50 additions & 87 deletions src/implementation/yaml-parser.adb
Expand Up @@ -367,7 +367,7 @@ package body Yaml.Parser is
P.Levels.Top.State := After_Implicit_Map_Start'Access;
else
if not Is_Empty (P.Header_Props) then
raise Parser_Error with "Alias may not have properties";
raise Parser_Error with "Alias may not have properties2";
end if;
-- alias is allowed on document root without '---'
P.Levels.Pop;
Expand Down Expand Up @@ -522,7 +522,7 @@ package body Yaml.Parser is
P.Header_Props := Default_Properties;
P.Levels.Top.State := After_Implicit_Map_Start'Access;
elsif not Is_Empty (P.Header_Props) then
raise Parser_Error with "Alias may not have properties";
raise Parser_Error with "Alias may not have properties1";
else
P.Levels.Pop;
end if;
Expand Down Expand Up @@ -672,7 +672,7 @@ package body Yaml.Parser is
P.Levels.Pop;
return False;
when Lexer.Alias =>
raise Parser_Error with "Alias may not have properties";
raise Parser_Error with "Alias may not have node properties";
when others =>
P.Levels.Pop;
return False;
Expand Down Expand Up @@ -718,28 +718,6 @@ package body Yaml.Parser is
Indentation => <>));
P.Current := Lexer.Next_Token (P.L);
return True;
when Lexer.Alias =>
E := Event'(Start_Position => P.Inline_Start,
End_Position => P.Current.End_Pos,
Kind => Alias,
Target => P.Pool.From_String (Lexer.Short_Lexeme (P.L)));
declare
Header_End : constant Mark := P.Current.Start_Pos;
begin
P.Current := Lexer.Next_Token (P.L);
if P.Current.Kind = Lexer.Map_Value_Ind then
P.Cached := E;
E := Event'(Start_Position => Header_End,
End_Position => Header_End,
Kind => Mapping_Start,
Collection_Properties => Default_Properties,
Collection_Style => Block);
P.Levels.Top.State := After_Implicit_Map_Start'Access;
else
P.Levels.Pop;
end if;
end;
return True;
when others =>
P.Levels.Top.State := After_Compact_Parent_Props'Access;
return False;
Expand All @@ -752,6 +730,10 @@ package body Yaml.Parser is
begin
P.Levels.Top.Indentation := Lexer.Recent_Indentation (P.L);
case P.Current.Kind is
when Lexer.Node_Property_Kind =>
P.Levels.Push ((State => Before_Node_Properties'Access,
Indentation => <>));
return False;
when Lexer.Indentation =>
P.Header_Start := P.Inline_Start;
P.Levels.Top.all :=
Expand Down Expand Up @@ -786,7 +768,27 @@ package body Yaml.Parser is
P.Levels.Top.State := After_Implicit_Map_Start'Access;
return True;
when Lexer.Alias =>
raise Parser_Error with "Alias may not have node properties";
E := Event'(Start_Position => P.Inline_Start,
End_Position => P.Current.End_Pos,
Kind => Alias,
Target => P.Pool.From_String (Lexer.Short_Lexeme (P.L)));
declare
Header_End : constant Mark := P.Current.Start_Pos;
begin
P.Current := Lexer.Next_Token (P.L);
if P.Current.Kind = Lexer.Map_Value_Ind then
P.Cached := E;
E := Event'(Start_Position => Header_End,
End_Position => Header_End,
Kind => Mapping_Start,
Collection_Properties => Default_Properties,
Collection_Style => Block);
P.Levels.Top.State := After_Implicit_Map_Start'Access;
else
P.Levels.Pop;
end if;
end;
return True;
when Lexer.Scalar_Token_Kind =>
E := Event'(Start_Position => P.Inline_Start,
End_Position => P.Current.End_Pos,
Expand Down Expand Up @@ -865,49 +867,16 @@ package body Yaml.Parser is

function After_Block_Parent_Props (P : in out Class;
E : out Event) return Boolean is
Header_End : Mark;
begin
P.Levels.Top.Indentation := Lexer.Recent_Indentation (P.L);
case P.Current.Kind is
when Lexer.Indentation =>
P.Header_Start := P.Inline_Start;
P.Levels.Top.all := (At_Block_Indentation'Access,
P.Levels.Element (P.Levels.Length - 2).Indentation);
P.Levels.Push ((State => Before_Block_Indentation'Access,
when Lexer.Node_Property_Kind =>
P.Levels.Push ((State => Before_Node_Properties'Access,
Indentation => <>));
return False;
when Lexer.Stream_End | Lexer.Document_End | Lexer.Directives_End =>
E := Event'(Start_Position => P.Inline_Start,
End_Position => P.Current.Start_Pos,
Kind => Scalar,
Scalar_Properties => P.Inline_Props,
Scalar_Style => Plain,
Content => Text.Empty);
P.Inline_Props := Default_Properties;
P.Levels.Pop;
return True;
when Lexer.Map_Value_Ind =>
raise Parser_Error with
"Compact notation not allowed after implicit key";
when Lexer.Alias =>
E := Event'(Start_Position => P.Inline_Start,
End_Position => P.Current.End_Pos,
Kind => Alias,
Target => P.Pool.From_String (Lexer.Short_Lexeme (P.L)));
Header_End := P.Current.Start_Pos;
P.Current := Lexer.Next_Token (P.L);
if P.Current.Kind = Lexer.Map_Value_Ind then
P.Cached := E;
E := Event'(Start_Position => Header_End,
End_Position => Header_End,
Kind => Mapping_Start,
Collection_Properties => Default_Properties,
Collection_Style => Block);
P.Levels.Top.State := After_Implicit_Map_Start'Access;
else
P.Levels.Pop;
end if;
return True;
when Lexer.Scalar_Token_Kind =>
E := Event'(Start_Position => P.Inline_Start,
End_Position => P.Current.End_Pos,
Expand All @@ -916,38 +885,16 @@ package body Yaml.Parser is
Scalar_Style => To_Style (P.Current.Kind),
Content => Lexer.Current_Content (P.L));
P.Inline_Props := Default_Properties;
Header_End := P.Current.Start_Pos;
P.Current := Lexer.Next_Token (P.L);
if P.Current.Kind = Lexer.Map_Value_Ind then
raise Parser_Error with
"Compact notation not allowed after implicit key";
end if;
P.Levels.Pop;
return True;
when Lexer.Flow_Map_Start =>
E := Event'(Start_Position => P.Inline_Start,
End_Position => P.Current.End_Pos,
Kind => Mapping_Start,
Collection_Properties => P.Inline_Props,
Collection_Style => Flow);
P.Inline_Props := Default_Properties;
P.Levels.Top.State := After_Flow_Map_Sep'Access;
P.Current := Lexer.Next_Token (P.L);
return True;
when Lexer.Flow_Seq_Start =>
E := Event'(Start_Position => P.Inline_Start,
End_Position => P.Current.End_Pos,
Kind => Sequence_Start,
Collection_Properties => P.Inline_Props,
Collection_Style => Flow);
P.Inline_Props := Default_Properties;
P.Levels.Top.State := After_Flow_Seq_Sep'Access;
P.Current := Lexer.Next_Token (P.L);
return True;
when others =>
raise Parser_Error with
"Unexpected token (expected newline or flow item start): " &
P.Current.Kind'Img;
P.Levels.Top.State := After_Compact_Parent_Props'Access;
return False;
end case;
end After_Block_Parent_Props;

Expand Down Expand Up @@ -1091,8 +1038,14 @@ package body Yaml.Parser is
E : out Event) return Boolean is
begin
case P.Current.Kind is
when Lexer.Node_Property_Kind =>
P.Levels.Push ((State => Before_Node_Properties'Access,
Indentation => <>));
when Lexer.Alias =>
raise Parser_Error with "Alias may not have node properties";
E := Event'(Start_Position => P.Inline_Start,
End_Position => P.Current.End_Pos,
Kind => Alias,
Target => P.Pool.From_String (Lexer.Short_Lexeme (P.L)));
when Lexer.Flow_Scalar_Token_Kind =>
E := Event'(Start_Position => P.Inline_Start,
End_Position => P.Current.End_Pos,
Expand Down Expand Up @@ -1269,6 +1222,16 @@ package body Yaml.Parser is
E : out Event) return Boolean is
begin
case P.Current.Kind is
when Lexer.Node_Property_Kind =>
P.Levels.Push ((State => Before_Node_Properties'Access,
Indentation => <>));
when Lexer.Alias =>
E := Event'(Start_Position => P.Inline_Start,
End_Position => P.Current.End_Pos,
Kind => Alias,
Target => P.Pool.From_String (Lexer.Short_Lexeme (P.L)));
P.Current := Lexer.Next_Token (P.L);
P.Levels.Pop;
when Lexer.Scalar_Token_Kind =>
E := Event'(Start_Position => P.Inline_Start,
End_Position => P.Current.End_Pos,
Expand Down Expand Up @@ -1611,7 +1574,7 @@ package body Yaml.Parser is
E := Event'(Start_Position => P.Current.Start_Pos,
End_Position => P.Current.Start_Pos,
Kind => Annotation_End);
P.Levels.Top.State := Before_Node_Properties'Access;
P.Levels.Pop;
return True;
end After_Annotation;

Expand Down

0 comments on commit b1fa9c9

Please sign in to comment.