Jelajahi Sumber

JSSpecCompiler: Parse <sup> as raising a number to a power in xspec mode

Dan Klishch 1 tahun lalu
induk
melakukan
bd26e5dce9

+ 2 - 1
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/AST/AST.h

@@ -253,7 +253,8 @@ protected:
     F(MemberAccess)                   \
     F(Minus)                          \
     F(Multiplication)                 \
-    F(Plus)
+    F(Plus)                           \
+    F(Power)
 
 #define NAME(name) name,
 #define STRINGIFY(name) #name##sv,

+ 10 - 1
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Parser/Lexer.cpp

@@ -106,6 +106,7 @@ void tokenize_string(SpecificationParsingContext& ctx, XML::Node const* node, St
 
 enum class TreeType {
     AlgorithmStep,
+    NestedExpression,
     Header,
 };
 
@@ -175,6 +176,14 @@ void tokenize_tree(SpecificationParsingContext& ctx, TokenizerState& state, XML:
                     return;
                 }
 
+                if (element.name == tag_sup) {
+                    tokens.append({ TokenType::Superscript, ""sv, move(child_location) });
+                    tokens.append({ TokenType::ParenOpen, ""sv, move(child_location) });
+                    tokenize_tree(ctx, state, child, TreeType::NestedExpression);
+                    tokens.append({ TokenType::ParenClose, ""sv, move(child_location) });
+                    return;
+                }
+
                 if (tree_type == TreeType::Header && element.name == tag_span) {
                     auto element_class = get_attribute_by_name(child, attribute_class);
                     if (element_class != class_secnum)
@@ -207,7 +216,7 @@ void tokenize_tree(SpecificationParsingContext& ctx, TokenizerState& state, XML:
             [&](auto const&) {});
     }
 
-    if (tokens.size() && tokens.last().type == TokenType::MemberAccess)
+    if (tree_type == TreeType::AlgorithmStep && tokens.size() && tokens.last().type == TokenType::MemberAccess)
         tokens.last().type = TokenType::Dot;
 }
 }

+ 1 - 0
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Parser/Lexer.h

@@ -22,6 +22,7 @@ inline constexpr StringView tag_ol = "ol"sv;
 inline constexpr StringView tag_p = "p"sv;
 inline constexpr StringView tag_span = "span"sv;
 inline constexpr StringView tag_specification = "specification"sv;
+inline constexpr StringView tag_sup = "sup"sv;
 inline constexpr StringView tag_var = "var"sv;
 
 inline constexpr StringView attribute_aoid = "aoid"sv;

+ 1 - 0
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Parser/Token.h

@@ -46,6 +46,7 @@ constexpr i32 closing_bracket_precedence = 18;
     F(Plus, 6, Invalid, Plus, Invalid, "plus")                                       \
     F(SectionNumber, -1, Invalid, Invalid, Invalid, "section number")                \
     F(String, -1, Invalid, Invalid, Invalid, "string literal")                       \
+    F(Superscript, 4, Invalid, Power, Invalid, "subscript")                          \
     F(UnaryMinus, 3, Minus, Invalid, Invalid, "unary minus")                         \
     F(Undefined, -1, Invalid, Invalid, Invalid, "constant")                          \
     F(Word, -1, Invalid, Invalid, Invalid, "word")

+ 1 - 1
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Tests/spec-single-function-simple.xml

@@ -24,7 +24,7 @@
                         <emu-xref aoid="𝔽"><a href="https://tc39.es/ecma262/#𝔽">𝔽</a></emu-xref>
                         (
                         <emu-xref aoid="floor"><a href="https://tc39.es/ecma262/#eqn-floor">floor</a></emu-xref>
-                        (<var>nowNs</var> / 1000000)).
+                        (<var>nowNs</var> / 10<sup>6</sup>)).
                      </li>
                   </ol>
                </emu-alg>

+ 3 - 1
Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Tests/spec-single-function-simple.xml.expectation

@@ -17,5 +17,7 @@ TreeList
         UnresolvedReference floor
         BinaryOperation Division
           Var nowNs
-          MathematicalConstant 1000000
+          BinaryOperation Power
+            MathematicalConstant 10
+            MathematicalConstant 6