From 1f10a91f76209dbeb350231a0efe3eda47759783 Mon Sep 17 00:00:00 2001 From: Felix Andreas Date: Fri, 19 Jun 2020 23:39:30 +0200 Subject: [PATCH 1/2] initial sequence parser --- latticejson/madx.lark | 6 ++++-- latticejson/parse.py | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/latticejson/madx.lark b/latticejson/madx.lark index 4679f99..79d3f6d 100644 --- a/latticejson/madx.lark +++ b/latticejson/madx.lark @@ -12,17 +12,19 @@ string : ESCAPED_STRING word : /[\w\.]+/ start : (_statement ";")* -_statement : element | lattice | command | assignment +_statement : element | lattice | sequence | command | assignment element : word ":" [word] ("," attribute)* ","? attribute : word ("=" | ":=") (expr | string) - lattice : word ":" "LINE"i "=" arrangement arrangement : [int "*"] [/-/] "(" object ("," object)* ")" ?object : ref_name | arrangement ref_name : [int "*"] [/-/] word +sequence : word ":" "SEQUENCE"i ("," attribute)* ";" content "ENDSEQUENCE"i +content : (element ";")* + command : word ("," (word | string | attribute))* // Arithmeitc Expressions diff --git a/latticejson/parse.py b/latticejson/parse.py index b93b21e..5e20f98 100644 --- a/latticejson/parse.py +++ b/latticejson/parse.py @@ -89,6 +89,7 @@ def transform(self, tree): def element(self, name, type_, *attributes): self.elements[name.upper()] = type_.upper(), dict(attributes) + return name def attribute(self, name, value): return name.upper(), value @@ -129,7 +130,9 @@ def command(self, *items): @v_args(inline=True) class MADXTransformer(ArithmeticTransformer, AbstractLatticeFileTransformer): - pass + def sequence(self, name, *items): + *_, arangement = items + self.lattices[name.upper()] = arangement.children @v_args(inline=True) From 2989290a8efbc0ee4e68954502f3b5d2fc270d1d Mon Sep 17 00:00:00 2001 From: Felix Andreas Date: Fri, 19 Jun 2020 23:58:00 +0200 Subject: [PATCH 2/2] update --- latticejson/madx.lark | 4 ++-- latticejson/parse.py | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/latticejson/madx.lark b/latticejson/madx.lark index 79d3f6d..bbaa152 100644 --- a/latticejson/madx.lark +++ b/latticejson/madx.lark @@ -22,8 +22,8 @@ arrangement : [int "*"] [/-/] "(" object ("," object)* ")" ?object : ref_name | arrangement ref_name : [int "*"] [/-/] word -sequence : word ":" "SEQUENCE"i ("," attribute)* ";" content "ENDSEQUENCE"i -content : (element ";")* +sequence : word ":" "SEQUENCE"i ("," attribute)* ";" seq_elements "ENDSEQUENCE"i +seq_elements: (element ";")* command : word ("," (word | string | attribute))* diff --git a/latticejson/parse.py b/latticejson/parse.py index 5e20f98..2858dfc 100644 --- a/latticejson/parse.py +++ b/latticejson/parse.py @@ -131,8 +131,11 @@ def command(self, *items): @v_args(inline=True) class MADXTransformer(ArithmeticTransformer, AbstractLatticeFileTransformer): def sequence(self, name, *items): - *_, arangement = items - self.lattices[name.upper()] = arangement.children + *attributes, elements = items + self.lattices[name.upper()] = elements + + def seq_elements(self, *elements): + return list(elements) @v_args(inline=True)