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

New recipe: Case-insensitivity #233

Merged
merged 4 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions hugo/content/docs/recipes/lexis/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Lexis
weight: 100
---
Lotes marked this conversation as resolved.
Show resolved Hide resolved
50 changes: 50 additions & 0 deletions hugo/content/docs/recipes/lexis/case-insensitive-languages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
title: Case-insensitive languages
weight: 100
---

Case-insensitive languages are more suitable for beginners, because they don't have to worry about the case of identifiers and keywords.
Lotes marked this conversation as resolved.
Show resolved Hide resolved

In Langium, there are basically two options you can choose from:

* you can either make Langium case-insensitive by configuration
* or you can include case-insensitivity only where you need it

In both ways you need to take care of identifiers and cross-references.
Lotes marked this conversation as resolved.
Show resolved Hide resolved

## Case-insensitivity by Langium

To make Langium case-insensitive, you have to set the `caseInsensitive` option to `true` in the `LangiumConfig` object which is located in the `langium-config.json` file at the root of your Langium project. You can set this up for every single language.

```json
{
"projectName": "HelloWorld",
"languages": [{
"id": "hello-world",
"caseInsensitive": true, //here
//...
}],
//...
}
```
Lotes marked this conversation as resolved.
Show resolved Hide resolved

## Case-insensitivity on demand

If you want to include case-insensitivity only where you need it, you can use the `i` flag inside of your grammar's regular expressions

```ts
//instead of
Rule: 'keyword';
//use
Rule: /keyword/i;
```
Lotes marked this conversation as resolved.
Show resolved Hide resolved

## Case-insensitivity for identifiers and cross-references

But be aware of that both ways will only take care of all the keywords in your grammar. If you want identifiers and cross-references to be case-insensitive as well, you have to adjust your scoping for each cross-reference case. This can be accomplished by setting the `caseInsensitive` option to `true` within the options when you are creating a new scope object.

There are several implementations of scopes. One is `MapScope`:
Lotes marked this conversation as resolved.
Show resolved Hide resolved

```ts
new MapScope(descriptions, parentScope, {caseInsensitive: true});
Lotes marked this conversation as resolved.
Show resolved Hide resolved
```
Loading