Skip to content
Draft
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
2 changes: 1 addition & 1 deletion PowerShellSyntax.tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@
</dict>
<dict>
<key>match</key>
<string>(?&lt;!\w|-|\.)((?i:begin|break|catch|clean|continue|data|default|define|do|dynamicparam|else|elseif|end|exit|finally|for|from|if|in|inlinescript|parallel|param|process|return|sequence|switch|throw|trap|try|until|var|while)|%|\?)(?!\w)</string>
<string>(?&lt;!\w|-|\.)((?i:begin|break|catch|clean|continue|data|default|define|dispose|do|dynamicparam|else|elseif|end|exit|finally|for|from|if|in|inlinescript|parallel|param|process|return|sequence|switch|throw|trap|try|until|var|while)|%|\?)(?!\w)</string>
<key>name</key>
<string>keyword.control.powershell</string>
</dict>
Expand Down
3 changes: 3 additions & 0 deletions examples/advancedFunction.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,7 @@ function Verb-Noun
Clean
{
}
Dispose
{
}
}
53 changes: 43 additions & 10 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
"node": "*"
},
"dependencies": {
"atom-grammar-test": "^0.6.3",
"fast-plist": "0.1.2",
"atom-grammar-test": "^0.6.3"
"vscode-oniguruma": "^2.0.1",
"vscode-textmate": "^9.3.2"
},
"scripts": {
"build-grammar": "node ./tools/build-grammar.js ./PowerShellSyntax.tmLanguage ./grammars/powershell.tmLanguage.json"
Expand Down
3 changes: 3 additions & 0 deletions parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const fs = require('fs');
const grammar = require('./grammars/powershell.tmLanguage.json');
console.log(Object.keys(grammar));
2 changes: 2 additions & 0 deletions run-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const test = require('atom-grammar-test');
test('./grammars/powershell.tmLanguage.json', './spec/testfiles/syntax_test_Function.ps1');
3 changes: 3 additions & 0 deletions spec/testfiles/syntax_test_Function.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,7 @@ function Verb-Noun {
Clean {
# <- keyword.control.powershell
}
Dispose {
# <- keyword.control.powershell
}
}
4 changes: 4 additions & 0 deletions test-grammar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const fs = require('fs');
const plist = require('fast-plist');
const grammar = plist.parse(fs.readFileSync('PowerShellSyntax.tmLanguage', 'utf8'));
// well, fast-plist only parses plist. We need a regex engine.
47 changes: 47 additions & 0 deletions test-tokenizer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const fs = require('fs');
const path = require('path');
const vsctm = require('vscode-textmate');
const oniguruma = require('vscode-oniguruma');

const wasmBin = fs.readFileSync(path.join(__dirname, 'node_modules', 'vscode-oniguruma', 'release', 'onig.wasm'));

const vscodeOnigurumaLib = oniguruma.loadWASM(wasmBin.buffer).then(() => {
return {
createOnigScanner(patterns) { return new oniguruma.OnigScanner(patterns); },
createOnigString(s) { return new oniguruma.OnigString(s); }
};
});

const registry = new vsctm.Registry({
onigLib: vscodeOnigurumaLib,
loadGrammar: async (scopeName) => {
if (scopeName === 'source.powershell') {
const grammarPath = path.join(__dirname, 'grammars', 'powershell.tmLanguage.json');
const grammarContent = fs.readFileSync(grammarPath, 'utf8');
return vsctm.parseRawGrammar(grammarContent, grammarPath);
}
return null;
}
});

registry.loadGrammar('source.powershell').then(grammar => {
const text = fs.readFileSync(path.join(__dirname, 'spec', 'testfiles', 'syntax_test_Function.ps1'), 'utf8');
const lines = text.split(/\r\n|\n/);
let ruleStack = vsctm.INITIAL;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const lineTokens = grammar.tokenizeLine(line, ruleStack);
ruleStack = lineTokens.ruleStack;

if (line.includes('Clean')) {
console.log(`Line ${i + 1}: ${line}`);
lineTokens.tokens.forEach(token => {
const tokenText = line.substring(token.startIndex, token.endIndex);
if (tokenText.trim()) {
console.log(` Token: "${tokenText}"`);
console.log(` Scopes: ${token.scopes.join(', ')}`);
}
});
}
}
});
40 changes: 40 additions & 0 deletions test-tokenizer2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const fs = require('fs');
const path = require('path');
const vsctm = require('vscode-textmate');
const oniguruma = require('vscode-oniguruma');

const wasmBin = fs.readFileSync(path.join(__dirname, 'node_modules', 'vscode-oniguruma', 'release', 'onig.wasm'));

oniguruma.loadWASM(wasmBin.buffer).then(() => {
const registry = new vsctm.Registry({
onigLib: Promise.resolve({
createOnigScanner(patterns) { return new oniguruma.OnigScanner(patterns); },
createOnigString(s) { return new oniguruma.OnigString(s); }
}),
loadGrammar: async (scopeName) => {
const grammarPath = path.join(__dirname, 'grammars', 'powershell.tmLanguage.json');
return vsctm.parseRawGrammar(fs.readFileSync(grammarPath, 'utf8'), grammarPath);
}
});

registry.loadGrammar('source.powershell').then(grammar => {
const text = fs.readFileSync(path.join(__dirname, 'spec', 'testfiles', 'syntax_test_Function.ps1'), 'utf8');
const lines = text.split(/\r\n|\n/);
let ruleStack = vsctm.INITIAL;
for (let i = lines.length - 15; i < lines.length; i++) {
const line = lines[i];
if (!line) continue;
const lineTokens = grammar.tokenizeLine(line, ruleStack);
ruleStack = lineTokens.ruleStack;

console.log(`Line ${i + 1}: ${line}`);
lineTokens.tokens.forEach(token => {
const tokenText = line.substring(token.startIndex, token.endIndex);
if (tokenText.trim()) {
console.log(` Token: "${tokenText}"`);
console.log(` Scopes: ${token.scopes.join(', ')}`);
}
});
}
});
});