Przeglądaj źródła

special case to avoid unnecessary cat/chc nodes

Getty Ritter 3 lat temu
rodzic
commit
9224e24f8b
1 zmienionych plików z 13 dodań i 3 usunięć
  1. 13 3
      src/grammar.lalrpop

+ 13 - 3
src/grammar.lalrpop

@@ -59,8 +59,12 @@ pub Name: Name = {
 
 pub Expr: Expr = {
     <mut ts:(<Choice> "|")*> <t:Choice> => {
-        ts.push(t);
-        Expr::Chc(ts)
+        if ts.len() == 0 {
+            t.value
+        } else {
+            ts.push(t);
+            Expr::Chc(ts)
+        }
     }
 };
 
@@ -76,7 +80,13 @@ pub Choice: Choice = {
 };
 
 pub Term: Expr = {
-    (<Branch>)* => Expr::Cat(<>),
+    <mut bs:(<Branch>)*> => {
+        if bs.len() == 1 {
+            bs.pop().unwrap()
+        } else {
+            Expr::Cat(<>)
+        }
+    }
 };
 
 pub Branch: Expr = {