From a07cad2b84950e1943da61b5522d557e8172768f Mon Sep 17 00:00:00 2001 From: walter-weinmann Date: Sun, 23 Apr 2017 17:39:34 +0200 Subject: [PATCH 1/2] Minor refactoring. --- src/ocparse.yrl | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/ocparse.yrl b/src/ocparse.yrl index 8fe6aac..d769802 100644 --- a/src/ocparse.yrl +++ b/src/ocparse.yrl @@ -838,8 +838,8 @@ fold_bu(Fun, Ctx, PTree) when is_function(Fun, 2) -> try ocparse_util:pt_to_source(bottom_up, Fun, Ctx, 0, PTree) of {error, _} = Error -> Error; - {Cypher, null_fun = Ctx} -> - list_to_binary(string:strip(Cypher)); + {Source, null_fun = Ctx} -> + list_to_binary(string:strip(Source)); {_Output, NewCtx} -> NewCtx catch @@ -854,8 +854,8 @@ fold_td(Fun, Ctx, PTree) when is_function(Fun, 2) -> try ocparse_util:pt_to_source(top_down, Fun, Ctx, 0, PTree) of {error, _} = Error -> Error; - {Cypher, null_fun = Ctx} -> - list_to_binary(string:strip(Cypher)); + {Source, null_fun = Ctx} -> + list_to_binary(string:strip(Source)); {_Output, NewCtx} -> NewCtx catch @@ -875,16 +875,16 @@ source_to_pt([]) -> {parse_error, invalid_string}; source_to_pt(<<>>) -> {parse_error, invalid_string}; -source_to_pt(Cypher0) -> - Cypher = re:replace(Cypher0, "(^[ \r\n]+)|([ \r\n]+$)", "", +source_to_pt(Source0) -> + Source = re:replace(Source0, "(^[ \r\n]+)|([ \r\n]+$)", "", [global, {return, list}]), - [C | _] = lists:reverse(Cypher), - NCypher = if C =:= $; -> - Cypher; + [C | _] = lists:reverse(Source), + NSource = if C =:= $; -> + Source; true -> - string:strip(Cypher) + string:strip(Source) end, - case oclexer:string(NCypher) of + case oclexer:string(NSource) of {ok, Toks, _} -> case ocparse:parse(Toks) of {ok, PTree} -> From b836f6f02ea3a8c334a16aa84588e7c2f3f49ada Mon Sep 17 00:00:00 2001 From: walter-weinmann Date: Sun, 23 Apr 2017 17:55:02 +0200 Subject: [PATCH 2/2] Updating the examples. --- README.md | 237 +++++++++++++++++++++++++++++------------------------- 1 file changed, 126 insertions(+), 111 deletions(-) diff --git a/README.md b/README.md index d08ad04..cf1c1c1 100644 --- a/README.md +++ b/README.md @@ -18,27 +18,36 @@ RETURN m ```erlang 1> {ok, {ParseTree, Tokens}} = ocparse:source_to_pt("MATCH (m:Movie) WHERE m.title = 'The Matrix' RETURN m"). - {ok, {{cypher, - {statement, - {query, - {regularQuery, - {singleQuery, - [{clause, - {match,[], - {pattern, - [{patternPart,[], - {anonymousPatternPart,{patternElement,{...},...}}}]}, - {where, - {expression, - {orExpression,{xorExpression,{andExpression,...},[]},[]}}}}}, - {clause, - {return,[], - {returnBody, - {returnItems,[],[],[{returnItem,{...},...}]}, - [],[],[]}}}]}, - []}}}, + {regularQuery, + {singleQuery, + [{clause, + {match,[], + {pattern, + [{patternPart,[], + {patternElement, + {nodePattern,{symbolicName,"m"},{nodeLabels,[{...}]},[]}, + []}}]}, + {where, + {orExpression, + {xorExpression, + {andExpression, + {notExpression, + {comparisonExpression,{addOrSubtractExpression,...},[...]}, + []}, + []}, + []}, + []}}}}, + {clause, + {return,[], + {returnBody, + {returnItems,[],[], + [{returnItem, + {orExpression,{xorExpression,{...},...},[]}, + []}]}, + [],[],[]}}}]}, + []}, []}, [{'MATCH',1}, {'(',1}, @@ -62,32 +71,43 @@ RETURN m 2> ParseTree. {cypher, - {statement, - {query, - {regularQuery, - {singleQuery, - [{clause, - {match,[], - {pattern, - [{patternPart,[], - {anonymousPatternPart, - {patternElement, - {nodePattern,{variable,...},{...},...}, - []}}}]}, - {where, - {expression, + {regularQuery, + {singleQuery, + [{clause, + {match,[], + {pattern, + [{patternPart,[], + {patternElement, + {nodePattern, + {symbolicName,"m"}, + {nodeLabels,[{nodeLabel,{symbolicName,...}}]}, + []}, + []}}]}, + {where, + {orExpression, + {xorExpression, + {andExpression, + {notExpression, + {comparisonExpression, + {addOrSubtractExpression, + {multiplyDivideModuloExpression,{...},...}, + []}, + [{partialComparisonExpression,{...},...}]}, + []}, + []}, + []}, + []}}}}, + {clause, + {return,[], + {returnBody, + {returnItems,[],[], + [{returnItem, {orExpression, - {xorExpression, - {andExpression,{notExpression,{...},...},[]}, - []}, - []}}}}}, - {clause, - {return,[], - {returnBody, - {returnItems,[],[], - [{returnItem,{expression,{orExpression,...}},[]}]}, - [],[],[]}}}]}, - []}}}, + {xorExpression,{andExpression,{notExpression,...},[]},[]}, + []}, + []}]}, + [],[],[]}}}]}, + []}, []} ``` @@ -132,23 +152,62 @@ The output of the parse tree in the Erlang shell is shortened (cause not known). ```erlang {cypher, - {statement, - {query, - {regularQuery, - {singleQuery, - [{clause, - {match,[], - {pattern, - [{patternPart,[], - {anonymousPatternPart, - {patternElement, - {nodePattern, - {variable,{symbolicName,"m"}}, - {nodeLabels,[{nodeLabel,{labelName,{symbolicName,"Movie"}}}]}, + {regularQuery, + {singleQuery, + [{clause, + {match,[], + {pattern, + [{patternPart,[], + {patternElement, + {nodePattern, + {symbolicName,"m"}, + {nodeLabels,[{nodeLabel,{symbolicName,"Movie"}}]}, + []}, + []}}]}, + {where, + {orExpression, + {xorExpression, + {andExpression, + {notExpression, + {comparisonExpression, + {addOrSubtractExpression, + {multiplyDivideModuloExpression, + {powerOfExpression, + {unaryAddOrSubtractExpression, + {stringListNullOperatorExpression, + {propertyOrLabelsExpression, + {atom,{symbolicName,"m"}}, + [{propertyLookup,{symbolicName,"title"}}]}, + []}, + []}, + []}, []}, - []}}}]}, - {where, - {expression, + []}, + [{partialComparisonExpression, + {addOrSubtractExpression, + {multiplyDivideModuloExpression, + {powerOfExpression, + {unaryAddOrSubtractExpression, + {stringListNullOperatorExpression, + {propertyOrLabelsExpression, + {atom, + {literal,{stringLiteral,"'The Matrix'"}}}, + []}, + []}, + []}, + []}, + []}, + []}, + "="}]}, + []}, + []}, + []}, + []}}}}, + {clause, + {return,[], + {returnBody, + {returnItems,[],[], + [{returnItem, {orExpression, {xorExpression, {andExpression, @@ -160,65 +219,21 @@ The output of the parse tree in the Erlang shell is shortened (cause not known). {unaryAddOrSubtractExpression, {stringListNullOperatorExpression, {propertyOrLabelsExpression, - {atom,{variable,{symbolicName,"m"}}}, - [{propertyLookup, - {propertyKeyName,{symbolicName,"title"}}}]}, + {atom,{symbolicName,"m"}}, + []}, []}, []}, []}, []}, []}, - [{partialComparisonExpression, - {addOrSubtractExpression, - {multiplyDivideModuloExpression, - {powerOfExpression, - {unaryAddOrSubtractExpression, - {stringListNullOperatorExpression, - {propertyOrLabelsExpression, - {atom,{literal,{stringLiteral,"'The Matrix'"}}}, - []}, - []}, - []}, - []}, - []}, - []}, - "="}]}, + []}, []}, []}, []}, - []}}}}}, - {clause, - {return,[], - {returnBody, - {returnItems,[],[], - [{returnItem, - {expression, - {orExpression, - {xorExpression, - {andExpression, - {notExpression, - {comparisonExpression, - {addOrSubtractExpression, - {multiplyDivideModuloExpression, - {powerOfExpression, - {unaryAddOrSubtractExpression, - {stringListNullOperatorExpression, - {propertyOrLabelsExpression, - {atom,{variable,{symbolicName,"m"}}}, - []}, - []}, - []}, - []}, - []}, - []}, - []}, - []}, - []}, - []}, - []}}, - []}]}, - [],[],[]}}}]}, - []}}}, + []}, + []}]}, + [],[],[]}}}]}, + []}, []} ```