widcardw / asciimath-parser

https://asciimath.widcard.win
MIT License
13 stars 3 forks source link

Improve the structure of the syntax tree #16

Open widcardw opened 8 months ago

widcardw commented 8 months ago

After I have refactored the tests, there are some redundant flat wrappers in the generated syntax tree. Sometimes I even don't know why the flat wrapper exists.

https://github.com/widcardw/asciimath-parser/blob/f5a0bb1bbe163f7ac3c44e78634660ff9e99b213/packages/core/test/parser.test.ts#L44

https://github.com/widcardw/asciimath-parser/blob/f5a0bb1bbe163f7ac3c44e78634660ff9e99b213/packages/core/test/parser.test.ts#L103

https://github.com/widcardw/asciimath-parser/blob/f5a0bb1bbe163f7ac3c44e78634660ff9e99b213/packages/core/test/parser.test.ts#L166-L172

https://github.com/widcardw/asciimath-parser/blob/f5a0bb1bbe163f7ac3c44e78634660ff9e99b213/packages/core/test/parser.test.ts#L211

Although the redundant flats may not change the output of toTex function, it still needs to be improved.

widcardw commented 7 months ago

zmx0142857 says that in this part, the program should judge if the body of flat has only one child and the type of the child is still flat, then just remove it.

https://github.com/widcardw/asciimath-parser/blob/f5a0bb1bbe163f7ac3c44e78634660ff9e99b213/packages/core/src/parser.ts#L101-L118

That's to say add

if (body.length === 1 && body[0].type === NodeTypes.Flat)
  return body[0]

before the return. This needs to be tested.

widcardw commented 7 months ago

However, in the type declaration, the n cannot be a FlatNode, but n can be a list that contains a FlatNode. Maybe the problem is here.