Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor AST utilities #1914

Merged

Conversation

Omikhleia
Copy link
Member

@Omikhleia Omikhleia commented Nov 21, 2023

Proposal, motivated by #1866

  • Move existing AST utilities to SU.ast
    • Rationale: While the SILE AST is just a table with some structure (tied to SILE.process etc.), having some convenience function to manipulate is good -- and a must-have for inputter and package designers. The existing utilities were however scattered (some at the root of SU, one as an inputter method, another in the inputfilter package, etc.)...
  • Refactored as a separate utility file.
    • Rationale: We have lots of different/unrelated stuff in SU, a better organization makes maintenance of similar functions easier and is the path for curating those utilities progressively. Later, we might at a glance easily check these if we ever want to change the AST (add new constructs, make it more object-oriented, etc.)
    • Adding in-code documentation: follows VSCode Lua extension syntax (I tried LDoc initially but could not make it work with VSCode... So there's nothing definitive here, I just thought something would be better than nothing.)
  • Proposed additions (taken from my silex.ast layer)
    • SU.ast.extractFromTree - complements SU.ast.findInTree
    • SU.ast.createCommand - as in inputfilter, but in inputters etc. this is convenient (and we might not even have loaded any package yet, there, by nature)
    • SU.ast.createStructuredCommand - convenience when passing an array of nodes (avoiding useless nesting)
    • SU.ast.trimSubContent and SU.ast.processAsStructure - fairly useful esp. in XML parsing context, when the schema defines structured elements where text/spaces aren't expected (unless due do indentation, but to be ignored in the ouput).
  • Other changes (also from silex.ast)

@Omikhleia Omikhleia added this to the v0.15.0 milestone Nov 21, 2023
@Omikhleia Omikhleia added the enhancement Software improvement or feature request label Nov 25, 2023
@Omikhleia Omikhleia mentioned this pull request Nov 26, 2023
3 tasks
@alerque
Copy link
Member

alerque commented Dec 6, 2023

Adding in-code documentation: follows VSCode Lua extension syntax (I tried LDoc initially but could not make it work with VSCode... So there's nothing definitive here, I just thought something would be better than nothing.)

Something is definitely better than nothing. We'll likely be going the LDoc route eventually (c.f. #1337) but it will be much easier to adapt something than nothing!

---@param tree table AST tree
---@param command string command name
---@return table|nil AST command node
function ast.extractFromTree (tree, command)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think "extract" is the right verb here, at least to me that implies getting a node out in isolation, but also I would have assumed it left the original tree alone. This removes it from the tree, so "slice" or perhaps "extricate" might be a better name that suggests the original is being removed.

Copy link
Member Author

@Omikhleia Omikhleia Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe simply removeFromTree (and the @return becomes "Removed AST command node") ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could go with that I think.

Copy link
Member

@alerque alerque left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need a BREAKING CHANGE log message, but I can take care of that. Overall I think it's looking good. Just doing some tinkering on my end to see what I think of the details.

@alerque alerque removed the request for review from simoncozens December 6, 2023 12:28
@alerque alerque linked an issue Dec 6, 2023 that may be closed by this pull request
@Omikhleia
Copy link
Member Author

Omikhleia commented Dec 12, 2023

By the way @alerque I was about to ask whether we should actually split utilities in subfolders (rather than -xxx files), and I just see you felt alike :) 👍

Would that be ok if we go on splitting the utilities in subfolders? Currently we still have in the main file:

  • Things pertaining to type casting, options etc.
  • Things pertaining to error/warning etc.
  • Things pertaining to general utilities (map, max, etc.)
  • Things pertaining to unicode and characters
  • A bunch of other things likely...

Perhaps starting to split them would allow us to properly group them by "nature" and document them progressively?

@Omikhleia
Copy link
Member Author

Perhaps starting to split them would allow us to properly group them by "nature" and document them progressively?

That could be another issue addressed later, obviously, just asking here as it sounds quite logical.

@alerque
Copy link
Member

alerque commented Dec 12, 2023

Would that be ok if we go on splitting the utilities in subfolders?

Yes. Smaller files/modules with tighter scopes are almost always better for Lua in my opinion.

Omikhleia and others added 2 commits December 12, 2023 23:32
BREAKING CHANGE: For modules that rely on `SILE.utilities` (`SU`),
and in particular raw content handling functions `subContent()`,
`walkContent()`, `stripContentPos()`, `hasContent()`, and
`contentToString()`, these and similar functions have been moved into
`SILE.utilities.ast` (`SU.ast`). The `subContent()` implementation also
no longer adds id="stuff" attributes to everything.
@alerque alerque force-pushed the refactor-ast-utilities-develop branch from af91cd2 to 06d9ea4 Compare December 12, 2023 20:33
@alerque
Copy link
Member

alerque commented Dec 12, 2023

P.S. I'm also seriously scratching my head why we have "core" and "utilities". It seems to me the way our source organization is sorting out almost everything in "core" is just utilities. Basically anything that is just a function as opposed to a class than gets instantiated and maintains some sort of state as SILE runs.

@Omikhleia
Copy link
Member Author

Omikhleia commented Dec 13, 2023

P.S. I'm also seriously scratching my head why we have "core" and "utilities". It seems to me the way our source organization is sorting out almost everything in "core" is just utilities. Basically anything that is just a function as opposed to a class than gets instantiated and maintains some sort of state as SILE runs.

The line is blurry, indeed.
A few things stand apart, though and hint towards different natures:

  • line breaker, page builder, hyphenator, fonts = core algorithm (some are classes or alike, actually): pretty specific to the field of digital typesetting
  • color, units, nodes, frame... = low level common concepts and data structures
  • cli, repl... = their name says it all, running on command line
  • most current "utilities" are things one could work without
    • Many just avoid lots of code and/or make things uniform
    • Several could find their place in other pieces software actually (error handling with stack, string encoding or collation, etc.)

So there could be some different levels of concern and design here ;-)

@alerque alerque merged commit ed5a505 into sile-typesetter:develop Dec 13, 2023
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Software improvement or feature request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Standard way for extracting a node from the AST tree
2 participants