Skip to content

feat: add support for show-code and conditional code #44

New issue

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

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

Already on GitHub? # to your account

Merged
merged 4 commits into from
Jul 17, 2023

Conversation

javier-godoy
Copy link
Member

@javier-godoy javier-godoy commented Jul 14, 2023

show-source is implemented as described in #42

Support for conditional code (#43) uses C-inspired directives #if, #elif (else-if), #else and #endif

Syntax: #if variable operator value

  • Supported variables are "vaadin" and "flow". (Note that Vaadin 14 uses Flox 1.x.x)
  • Supported operators are lt, le, ne, eq, gt, ge (Operator names borrowed from Fortran)
  • Value is a one-digit (x), two-digit (x.y) or three-digit (x.y.z) version number

Implementation: The cleanupCode algorithm was improved in order to preprocess the sources before feeding them to prism.js. The implementation uses two stacks: a stack of guards (guards) and a stack of directives (the stack). Guards are three-valued: istrue, isfalse or wastrue.

When an #if directive is found, it's immediately pushed onto the directives stack. #elif and #else directives replace the top of the directives stack, but only if we can transition into them (in order to avoid malformed blocks such as if-else-else); otherwise the top of stack is replaced with error, which can only be popped. #endif pops the stack.

When an #if directive is found, the condition is evaluated and the result is pushed onto guards. #elif and #else directives replace the top of the guards stack with the evaluation result (#else evaluates to istrue), but only if the top of the guards stack isfalse; otherwise they replace the top of stack with wastrue. #endif pops the guards stack.

Directives are always removed from the rendered sources. Other lines (which are not directives) are removed if any guard in the stack isfalse or wastrue.

@javier-godoy javier-godoy requested review from mlopezFC and paodb July 14, 2023 20:03
@javier-godoy javier-godoy marked this pull request as draft July 14, 2023 20:13
@javier-godoy javier-godoy force-pushed the conditional-snippet branch from 3427c9e to f84ade8 Compare July 17, 2023 12:50
@javier-godoy javier-godoy marked this pull request as ready for review July 17, 2023 17:21
@javier-godoy javier-godoy requested a review from flang July 17, 2023 17:23
@javier-godoy javier-godoy force-pushed the conditional-snippet branch from f84ade8 to 5801c6a Compare July 17, 2023 19:02
Copy link
Member

@paodb paodb left a comment

Choose a reason for hiding this comment

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

LGTM

@paodb paodb merged commit 0a4b971 into master Jul 17, 2023
@paodb paodb deleted the conditional-snippet branch July 17, 2023 19:43
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants