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

Soft Parser: Initial implementation of SoftParser #328

Merged
merged 11 commits into from
Nov 28, 2024
Merged

Conversation

simerplaha
Copy link
Member

@simerplaha simerplaha commented Nov 25, 2024

Overview

Syntax error-insensitive

SoftParser always generates an AST, even if the code contains syntax errors. This ensures that LSP APIs (jump-to-definition, code completion etc) are always available.

  • Multiple Errors: The following screenshot is for demo only. It shows how multiple errors can be generated by a single parser run. These errors are required for "Code Actions" (no plans to replace errors reported by ralphc). It's also not tested yet, this version of the parser is only tested to ensure that an AST is always available.
    image
  • Unknown Tokens: Unknown syntax tokens are stored as Unresolved in the Syntax Tree.
    Unresolved(code = 🚀, index = SourceIndex(113,2,None))

Order-insensitive

The order of the code is not enforced. For example, the following code is parseable:

fn function() -> Bool {
   Contract MyContract() { }

   fn nestedFunction() -> ABC
}

Releases & Incremental implementation

Being error-insensitive allows SoftParser to be implemented and integrated incrementally. For example, the following is valid Ralph code, but let bool = true is parsed as Unresolved by SoftParser because it does not recognise this syntax yet.

fn function() -> Bool {
  let bool = true
}
  • This ensures that any future changes to Ralph syntax in Node will not delay the release of ralph-lsp with the latest Node version.
  • This also allows SoftParser to used by jump-definitions, code-completion etc, even though it is only partially implemented.

Convert SoftAST to Code

SoftAST can always be converted back to the original string code using the toCode() method call:

val code: String = ast.toCode()

toCode() can be also be invoked on any sub-tree, resulting in code just for that tree.

See Demo.scala.

Formatting

AST as String

Invoke the following to generate a String tree representation of the AST.

val tree: String = ast.toStringTree()

See Demo.scala.

Next steps

  • Pending documentation.
  • Instead of implementing formatting as a new feature, I think we should integrate this parser into the currently implemented APIs, so that existing features are complete before implementing on new features. WDYT?

@simerplaha simerplaha requested a review from tdroxler November 25, 2024 05:11
@tdroxler
Copy link
Member

Instead of implementing formatting as a new feature, I think we should integrate this parser into the currently implemented APIs, so that existing features are complete before implementing on new features. WDYT?

Sound's good to me

@tdroxler
Copy link
Member

I translated lexeme to see what was the french world: lexème
.
.
.
Now I add to google the definititon of lexème 😅 So thx to you I learnt a new word.

Copy link
Member

@tdroxler tdroxler left a comment

Choose a reason for hiding this comment

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

Amazing work

Base automatically changed from move_to_utils to master November 28, 2024 02:05
@simerplaha
Copy link
Member Author

Lexème sounds better in French 😆

@simerplaha simerplaha merged commit 4b91123 into master Nov 28, 2024
3 checks passed
@simerplaha simerplaha deleted the soft_parser branch November 28, 2024 02:08
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