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

Add support for rust #99

Merged
merged 6 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 20 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"fuse.js": "6.6.2",
"js-search": "2.0.0",
"mustache": "4.2.0",
"tree-sitter-rust": "0.20.1",
"vscode-languageserver-types": "3.17.2",
"web-tree-sitter": "^0.20.7"
},
Expand Down
5 changes: 5 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ const languages = [
dir: '',
wasm: 'python',
},
{
npm: 'tree-sitter-rust',
dir: '',
wasm: 'rust',
},
]

// Build wasm parsers for supported languages
Expand Down
2 changes: 2 additions & 0 deletions src/language/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { javaLanguage } from './javaLanguage.js'
import { phpLanguage } from './phpLanguage.js'
import { pythonLanguage } from './pythonLanguage.js'
import { rubyLanguage } from './rubyLanguage.js'
import { rustLanguage } from './rustLanguage.js'
import { tsxLanguage } from './tsxLanguage.js'
import { Language, LanguageName } from './types.js'

Expand All @@ -12,6 +13,7 @@ const languageByName: Record<LanguageName, Language> = {
c_sharp: csharpLanguage,
php: phpLanguage,
ruby: rubyLanguage,
rust: rustLanguage,
python: pythonLanguage,
}

Expand Down
114 changes: 114 additions & 0 deletions src/language/rustLanguage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { Language, TreeSitterSyntaxNode } from './types.js'

export const rustLanguage: Language = {
toParameterTypeName(node) {
switch (node.type) {
case 'raw_string_literal': {
return stringLiteral(node)
}
case 'string_literal': {
return stringLiteral(node)
}
case 'identifier': {
return node.text
}
default: {
throw new Error(`Unsupported node type ${node.type}`)
}
}
},
toParameterTypeRegExps(node) {
return stringLiteral(node)
},
toStepDefinitionExpression(node) {
const text = stringLiteral(node)
const hasRegExpAnchors = text[0] == '^' || text[text.length - 1] == '$'
return hasRegExpAnchors ? new RegExp(text) : text
},

defineParameterTypeQueries: [
`
(attribute_item
(meta_item
(identifier) @meta-name
arguments: (meta_arguments
[
((meta_item
(identifier) @name-key
value: (string_literal) @name
)(meta_item
(identifier) @value-key
value: (string_literal) @expression
))
((meta_item
(identifier) @value-key
value: (string_literal) @expression
)(meta_item
(identifier) @name-key
value: (string_literal) @name
))
]
)
)
(#eq? @meta-name "param")
(#eq? @name-key "name")
(#eq? @value-key "regex")
) @root
`,
],
defineStepDefinitionQueries: [
`
(attribute_item
(meta_item
(
(identifier) @meta-name
arguments: (meta_arguments
[
(string_literal) @expression
(meta_item
value: (string_literal) @expression)
(meta_item
(identifier)
value: (string_literal) @expression)
(meta_item
value: (raw_string_literal) @expression)
]
)
)
) @root
(#match? @meta-name "given|when|then")
)
`,
],

snippetParameters: {
int: { type: 'i32', name: 'i' },
float: { type: 'f32', name: 'f' },
word: { type: 'String' },
string: { type: 'String', name: 's' },
double: { type: 'f64', name: 'd' },
bigdecimal: { type: 'String', name: 'bigDecimal' },
byte: { type: 'u8', name: 'b' },
short: { type: 'i16', name: 's' },
long: { type: 'i64', name: 'l' },
biginteger: { type: 'String', name: 'bigInteger' },
'': { type: 'String', name: 'arg' },
},
defaultSnippetTemplate: `
#[{{ keyword }}(expr = "{{ expression }}")]
fn {{ #underscore }}{{ expression }}{{ /underscore }}(world: &mut TheWorld, {{ #parameters }}{{ #seenParameter }}, {{ /seenParameter }}{{ name }}: {{ type }}{{ /parameters }}) {
// {{ blurb }}
}
`,
}

function stringLiteral(node: TreeSitterSyntaxNode | null): string {
if (node === null) throw new Error('node cannot be null')
if (node.text[0] === 'r') return unescapeString(node.text.slice(2, -1))
return unescapeString(node.text.slice(1, -1))
}

// rust
function unescapeString(s: string): string {
return s.replace(/\\\\/g, '\\')
}
2 changes: 1 addition & 1 deletion src/language/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export type SnippetParameters = Readonly<Record<ParameterTypeName, SnippetParame

export type ParameterTypeMeta = Readonly<{ name: string; regexp: string }>

export const LanguageNames = ['java', 'tsx', 'c_sharp', 'php', 'python', 'ruby'] as const
export const LanguageNames = ['java', 'tsx', 'c_sharp', 'php', 'python', 'ruby', 'rust'] as const
export type LanguageName = typeof LanguageNames[number]

export type Source<L> = Readonly<{
Expand Down
5 changes: 5 additions & 0 deletions src/tree-sitter-node/NodeParserAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import Python from 'tree-sitter-python'
// @ts-ignore
import Ruby from 'tree-sitter-ruby'
// @ts-ignore
import Rust from 'tree-sitter-rust'
// @ts-ignore
import TypeScript from 'tree-sitter-typescript'

import { LanguageName, ParserAdapter } from '../language/types.js'
Expand Down Expand Up @@ -42,6 +44,9 @@ export class NodeParserAdapter implements ParserAdapter {
case 'python':
this.parser.setLanguage(Python)
break
case 'rust':
this.parser.setLanguage(Rust)
break
default:
throw new Error(`Unsupported language: ${languageName}`)
}
Expand Down
9 changes: 8 additions & 1 deletion test/language/ExpressionBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import { NodeParserAdapter } from '../../src/tree-sitter-node/NodeParserAdapter.
import { WasmParserAdapter } from '../../src/tree-sitter-wasm/WasmParserAdapter.js'

// List languages that support Cucumber Expressions here
const cucumberExpressionsSupport: Set<LanguageName> = new Set(['c_sharp', 'java', 'ruby', 'tsx'])
const cucumberExpressionsSupport: Set<LanguageName> = new Set([
'c_sharp',
'java',
'ruby',
'rust',
'tsx',
])

function defineContract(makeParserAdapter: () => ParserAdapter) {
let expressionBuilder: ExpressionBuilder
Expand Down Expand Up @@ -73,6 +79,7 @@ function defineContract(makeParserAdapter: () => ParserAdapter) {
: (expression as RegularExpression).regexp
)
const errors = result.errors.map((e) => e.message)

if (cucumberExpressionsSupport.has(languageName)) {
assert.deepStrictEqual(expressions, [
'a {uuid}',
Expand Down
14 changes: 14 additions & 0 deletions test/language/testdata/rust/ParameterTypes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use cucumber::Parameter;

#[derive(Debug, Default, Parameter)]
#[param(name = "planet", regex = "jupiter|mars|tellus")]
struct Planet(String)


#[derive(Debug, Default, Parameter)]
#[param(name = "uuid", regex = "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}")]
struct Uuid(String)

#[derive(Debug, Default, Parameter)]
#[param(name = "date", regex = "\\d{4}-\\d{2}-\\d{2}")]
struct Date(chrono::DateTime)
23 changes: 23 additions & 0 deletions test/language/testdata/rust/StepDefinitions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use cucumber::{given, then, when, World};


#[derive(Debug, Default, World)]
struct StepDefinitions {}

#[given(expr = "a {uuid}")]
fn a_uuid(world: &mut StepDefinitions, uuid: String) {}

#[given(expr = "a {date}")]
fn a_date(world: &mut StepDefinitions,date: chrono::DateTime) {}

#[given("a {planet}")]
fn a_date(world: &mut StepDefinitions,date: chrono::DateTime) {}

#[given(regex = r"^a regexp$")]
fn a_regexp(world: &mut StepDefinitions) {}

#[given(expr = "an {undefined-parameter}")]
fn an_undefined_parameter(world: &mut StepDefinitions, date: chrono::DateTime) {}

#[given("the bee's knees")]
fn the_bees_knees(world: &mut StepDefinitions) {}