Skip to content

Commit d641275

Browse files
committed
Add script to generete reference file from test file, small grammar fix, more tests
1 parent 5a59a5b commit d641275

9 files changed

Lines changed: 9237 additions & 353 deletions

‎PowerShellSyntax.YAML-tmLanguage‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,9 @@ repository:
572572
-
573573
comment: 'built-in variables; $args'
574574
match: '(\$)(?i:(_|args|consolefilename|error|event|eventsubscriber|executioncontext|false|foreach|home|host|input|lastexitcode|matches|myinvocation|nestedpromptlevel|null|pid|psboundparameters|pscmdlet|psculture|psdebugcontext|pshome|psitem|psscriptroot|psuiculture|psversiontable|pwd|sender|shellid|sourceargs|sourceeventargs|switch|this|true))\b'
575-
name: variable.language
575+
captures:
576+
'1': {name: punctuation.definition.variable}
577+
'2': {name: variable.language}
576578
-
577579
comment: 'built-in variables; ${args}'
578580
match: '(\$)(\{?)(?i:(_|args|consolefilename|error|event|eventsubscriber|executioncontext|false|foreach|home|host|input|lastexitcode|matches|myinvocation|nestedpromptlevel|null|pid|psboundparameters|pscmdlet|psculture|psdebugcontext|pshome|psitem|psscriptroot|psuiculture|psversiontable|pwd|sender|shellid|sourceargs|sourceeventargs|switch|this|true))\b(\}?)'

‎generateReference.js‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var fs = require('fs');
2+
var Registry = require('vscode-textmate').Registry;
3+
var yaml = require('js-yaml');
4+
var registry = new Registry();
5+
var ruleStack = null;
6+
var lastElementId = 0;
7+
var grammarPath = './grammars/PowerShellSyntax.tmLanguage'
8+
var referencePath = './referenceFile.yaml'
9+
var testFilePath = './test.ps1';
10+
11+
// read grammar file
12+
var registry = new Registry();
13+
var grammar = registry.loadGrammarFromPathSync(grammarPath);
14+
15+
// read test script
16+
var fileContents = require('fs').readFileSync(testFilePath).toString();
17+
var lines = fileContents.split(/\r\n|\r|\n/);
18+
19+
var ref = {"lines":[]};
20+
21+
// iterate through each line in the test script
22+
for (var i = 0; i < lines.length; i++) {
23+
var line = lines[i];
24+
25+
// skip empty lines
26+
if (line != '') {
27+
var tokens = [];
28+
var lineTokens = grammar.tokenizeLine(line, ruleStack);
29+
for (var y = 0; y < lineTokens.tokens.length; y++) {
30+
var token = lineTokens.tokens[y];
31+
var tokenString = line.substring(token.startIndex,token.endIndex);
32+
var scopes = token.scopes;
33+
tokens.push({"token":tokenString,"scopes":scopes});
34+
}
35+
ref.lines.push({"line":line,"tokens":tokens});
36+
ruleStack = lineTokens.ruleStack;
37+
}
38+
}
39+
40+
var refYaml = yaml.dump(ref);
41+
fs.writeFileSync(referencePath,refYaml);
42+
console.log("Reference file created!");

‎grammars/PowerShellSyntax-JSON-tmLanguage‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,14 @@
10581058
{
10591059
"comment": "built-in variables; $args",
10601060
"match": "(\\$)(?i:(_|args|consolefilename|error|event|eventsubscriber|executioncontext|false|foreach|home|host|input|lastexitcode|matches|myinvocation|nestedpromptlevel|null|pid|psboundparameters|pscmdlet|psculture|psdebugcontext|pshome|psitem|psscriptroot|psuiculture|psversiontable|pwd|sender|shellid|sourceargs|sourceeventargs|switch|this|true))\\b",
1061-
"name": "variable.language"
1061+
"captures": {
1062+
"1": {
1063+
"name": "punctuation.definition.variable"
1064+
},
1065+
"2": {
1066+
"name": "variable.language"
1067+
}
1068+
}
10621069
},
10631070
{
10641071
"comment": "built-in variables; ${args}",

‎grammars/PowerShellSyntax.cson‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,11 @@ repository:
901901
{
902902
comment: "built-in variables; $args"
903903
match: "(\\$)(?i:(_|args|consolefilename|error|event|eventsubscriber|executioncontext|false|foreach|home|host|input|lastexitcode|matches|myinvocation|nestedpromptlevel|null|pid|psboundparameters|pscmdlet|psculture|psdebugcontext|pshome|psitem|psscriptroot|psuiculture|psversiontable|pwd|sender|shellid|sourceargs|sourceeventargs|switch|this|true))\\b"
904-
name: "variable.language"
904+
captures:
905+
"1":
906+
name: "punctuation.definition.variable"
907+
"2":
908+
name: "variable.language"
905909
}
906910
{
907911
comment: "built-in variables; ${args}"

‎grammars/PowerShellSyntax.tmLanguage‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,8 +1629,19 @@
16291629
<string>built-in variables; $args</string>
16301630
<key>match</key>
16311631
<string>(\$)(?i:(_|args|consolefilename|error|event|eventsubscriber|executioncontext|false|foreach|home|host|input|lastexitcode|matches|myinvocation|nestedpromptlevel|null|pid|psboundparameters|pscmdlet|psculture|psdebugcontext|pshome|psitem|psscriptroot|psuiculture|psversiontable|pwd|sender|shellid|sourceargs|sourceeventargs|switch|this|true))\b</string>
1632-
<key>name</key>
1633-
<string>variable.language</string>
1632+
<key>captures</key>
1633+
<dict>
1634+
<key>1</key>
1635+
<dict>
1636+
<key>name</key>
1637+
<string>punctuation.definition.variable</string>
1638+
</dict>
1639+
<key>2</key>
1640+
<dict>
1641+
<key>name</key>
1642+
<string>variable.language</string>
1643+
</dict>
1644+
</dict>
16341645
</dict>
16351646
<dict>
16361647
<key>comment</key>

0 commit comments

Comments
 (0)