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

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

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 marked this pull request as draft July 14, 2023 20:13
@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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants