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

Allow optional capital letters and numbers in code Beta #295

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

adamwalz
Copy link
Contributor

This commit modifies the source code generation grammars to allow the user to select whether capital letters and numbers should be added to a lesson while maintaining syntactically accurate generated code. It allows language-specific code conventions to be maintained such as only generating rust structs when capital letters are included, and allows for language-specific numeric literals.

collage

And typo in static typed number literal
This commit modifies the source code generation grammars to allow the
user to select whether capital letters and numbers should be added to a
lesson while maintaining syntactically accurate generated code. It allows
language-specific code conventions to be maintained such as only generating
rust structs when capital letters are included, and allows for language-specific
numeric literals.
@adamwalz adamwalz marked this pull request as draft October 14, 2024 02:17
@adamwalz
Copy link
Contributor Author

This PR is a proof-of-concept just to start a conversation around the idea. It is not meant to be merged in this state.

Personally I'm not overly happy with with how this refactor turned out. I was hoping that more of the grammar would be agnostic to capital letters and numbers, but it grew into what is basically 4 grammars per language. If this grammar approach ends up being used for optional capital letters and numbers it might be easier to separate the grammars into 4 files rather than have a single file for all versions.

What I do like about this approach is that it maintains language idiocies in a way that I think would be hard or impossible if the capitalization and adding numbers were performed as a post-transformation.

@aradzie
Copy link
Owner

aradzie commented Oct 15, 2024

Omg, this feature is awesome!
Please let me finish another feature that I am working on right now, the custom theme designer. It will take a day or two.
Then let's work together on this PR.

@aradzie
Copy link
Owner

aradzie commented Oct 20, 2024

I am sorry for the late reply. I was having a really good streak while working on themes, did not want to interrupt myself.

I was thinking about adding conditional grammar branches, similar to Rust conditional compilation.

In Rust we can write

#[cfg(target_os = "macos")]
fn macos_only() {
  // ...
}

In Keybr we could write

#[feature "upper-case"]
c_func_id_upper_case ->
    "Next"
  | "Prev
   ...

Or, where this is not enough, we could mark a portion of grammar with a feature flag:

c_func_id_suffix ->
    "abort"
  | "bsearch"
  | "calloc"
  | { :feature("upper-case") // new syntax!
      "Next"
    | "Prev
    }

It is an experimental syntax straight out of my head, it does not exist yet:

   "{" _ ":feature" _ "(" _ feature_id _ ")" ... optional rules ... "}"

(The underscore characters indicates optional whitespace.)

Here the curly braces are used to wrap an optional list of rules. Each opening curly brace is followed by a feature flag name, then a list of rules, then a closing brace. The algorithm should scan all such feature flags in a grammar and present them to the user in the settings. If the user chooses to disable a feature, then the algorithm should just remove that fragment from the grammar. It should be as simple as removing a branch from a tree.

The same syntax could be used for syntax highlighting:

c_func_id_suffix -> { :syntax("id")
  | "bsearch"
  | "calloc"
}

or

c_type -> { :syntax("keyword") ( "char" | "int" | "long" | "short" | "void" ) } [ "*" [ "*" ] ] ;

Here we use curly braces with with different flags to mark a generated text fragment with color attributes.

@aradzie
Copy link
Owner

aradzie commented Oct 20, 2024

I am really eager to add more languages to keybr. That is a good PR! However, you mixed two different features in the same PR:

  1. New languages syntaxes fro Rust and Go
  2. Optional uppercase and number characters.

If you give me a PR with just new syntaxes, I will merge it right away. Then we could iterate on adding the remaining features.

Thank you!

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