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 3 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
2 changes: 1 addition & 1 deletion hugo/content/docs/recipes/builtin-library.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "Builtin Libraries"
weight: 100
weight: 200
---

Languages usually offer their users some high-level programming features that they do not have to define themselves.
Expand Down
4 changes: 4 additions & 0 deletions hugo/content/docs/recipes/lexing/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Lexing
weight: 50
---
54 changes: 54 additions & 0 deletions hugo/content/docs/recipes/lexing/case-insensitive-languages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
title: Case-insensitive languages
weight: 100
---

Some programming languages such as SQL or Structured Text use case insensitivity to provide more flexibility when writing code. For example most SQL databases accept select statements starting with `select`, `SELECT` or even `SeLeCt`.

In case you want to provide your users the same flexibility with your language, there are different levels of case-insensitivity in Langium:

* You can make Langium's parser completely case insensitive using the language configuration
* You can include case-insensitivity for specific terminal rules
* You can make cross references case insensitive

All of these options can be enabled independent of one another.

## Case-insensitivity by configuration

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.

```json5
{
...
"languages": [
{
"id": "hello-world",
"caseInsensitive": true, // <-- makes the specified language case insensitive
...
},
...
],
...
}
```

## 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
Lotes marked this conversation as resolved.
Show resolved Hide resolved
// append `i` to any regex to make it case insensitive
terminal ID: /[A-Z]/i;
```

Note that regular expressions can only be used inside of terminal rules.

## 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. `MapScope` is very commonly used:

```ts
new MapScope(descriptions, parentScope, { caseInsensitive: true });
```
2 changes: 1 addition & 1 deletion hugo/content/docs/recipes/scoping/_index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "Scoping"
weight: 0
weight: 100
---

You likely know scopes from programming, where some variables are only available from certain areas (such as blocks) in your program. For example, take the short Typescript snippet below. Based on the block (scope) where a variable is declared, it may or may not be available at another location in the same program.
Expand Down
Loading