Skip to content

Commit

Permalink
Add R language
Browse files Browse the repository at this point in the history
  • Loading branch information
rien committed Oct 27, 2023
1 parent bec8522 commit 29eb8bd
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@
[submodule "parsers/modelica"]
path = parsers/modelica
url = [email protected]:OpenModelica/tree-sitter-modelica.git
[submodule "parsers/r"]
path = parsers/r
url = [email protected]:r-lib/tree-sitter-r.git
branch = next
13 changes: 7 additions & 6 deletions lib/src/lib/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export class LanguagePicker {
new ProgrammingLanguage("java", [".java"]),
new ProgrammingLanguage("javascript", [".js"]),
new ProgrammingLanguage("elm", [".elm"]),
new ProgrammingLanguage("r", [".r", ".rdata", ".rds", ".rda"]),
new ProgrammingLanguage("typescript", [".ts"]),
new ProgrammingLanguage("tsx", [".tsx"]),
new CustomTokenizerLanguage("char", [".txt", ".md"], async self => {
Expand All @@ -128,8 +129,8 @@ export class LanguagePicker {
constructor() {
for (const language of LanguagePicker.languages) {
for (const extension of language.extensions) {
this.byExtension.set(extension, language);
this.byName.set(language.name, language);
this.byExtension.set(extension.toLowerCase(), language);
this.byName.set(language.name.toLowerCase(), language);
}
}
}
Expand All @@ -146,10 +147,10 @@ export class LanguagePicker {
let maxCount = 0;
let language: Language | undefined = undefined;
for (const file of files) {
const count = (counts.get(file.extension) ?? 0) + 1;
const count = (counts.get(file.extension.toLowerCase()) ?? 0) + 1;
if (count > maxCount) {
maxCount = count;
language = this.byExtension.get(file.extension);
language = this.byExtension.get(file.extension.toLowerCase());
}
counts.set(file.extension, count);
}
Expand All @@ -169,9 +170,9 @@ export class LanguagePicker {
* @param name
*/
public async findLanguage(name: string): Promise<Language> {
let language = this.byName.get(name);
let language = this.byName.get(name.toLowerCase());
if (language == null) {
const proglang = new ProgrammingLanguage(name, []);
const proglang = new ProgrammingLanguage(name.toLowerCase(), []);
// Try to load the language module to see if it exists,
// will throw an error if it does not exist.
await proglang.loadLanguageModule();
Expand Down
1 change: 1 addition & 0 deletions lib/src/test/tokenizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const languageFiles = {
"python": "../samples/python/caesar.py",
"php": "../samples/php/caesar.php",
"modelica": "../samples/modelica/sample.mo",
"r": "../samples/r/caesar.R",
"tsx": "../samples/tsx/sample.tsx",
"typescript": "../samples/typescript/caesar.ts",
} as {[key: string]: string};
Expand Down
1 change: 1 addition & 0 deletions parsers/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"modelica/binding.gyp",
"php/binding.gyp",
"python/binding.gyp",
"r/binding.gyp",
"typescript/binding.gyp"
]
}
3 changes: 3 additions & 0 deletions parsers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ module.exports.php.nodeTypeInfo = require("./php/src/node-types.json");
module.exports.python = require("./build/Release/tree_sitter_python_binding");
module.exports.python.nodeTypeInfo = require("./python/src/node-types.json");

module.exports.r = require("./build/Release/tree_sitter_r_binding");
module.exports.r.nodeTypeInfo = require("./r/src/node-types.json");

const typescript_tsx = require("./build/Release/tree_sitter_typescript_binding");
module.exports.typescript = typescript_tsx.typescript;
module.exports.typescript.nodeTypeInfo = require("./typescript/typescript/src/node-types.json");
Expand Down
1 change: 1 addition & 0 deletions parsers/r
Submodule r added at 1e9eaa
25 changes: 25 additions & 0 deletions samples/r/caesar.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
caesar <- function(x, key)
{
# if key is negative, wrap to be positive
if (key < 0) {
key <- 26 + key
}

old <- paste(letters, LETTERS, collapse="", sep="")
new <- paste(substr(old, key * 2 + 1, 52), substr(old, 1, key * 2), sep="")
chartr(old, new, x)
}

# simple examples from description
print(caesar("hi",2))
print(caesar("hi",20))

# more advanced example
key <- 3
plaintext <- "The five boxing wizards jump quickly."
cyphertext <- caesar(plaintext, key)
decrypted <- caesar(cyphertext, -key)

print(paste(" Plain Text: ", plaintext, sep=""))
print(paste(" Cypher Text: ", cyphertext, sep=""))
print(paste("Decrypted Text: ", decrypted, sep=""))

0 comments on commit 29eb8bd

Please sign in to comment.