tree-sitter-scpi
v0.1.4
Published
Tree-sitter grammar for SCPI (Standard Commands for Programmable Instruments)
Maintainers
Readme
tree-sitter-scpi
Tree-sitter grammar for SCPI (Standard Commands for Programmable Instruments).
Overview
This grammar provides syntax parsing for SCPI commands used in programmable instruments. It supports:
- Common commands: Asterisk-prefixed commands like
*IDN?,*RST - Hierarchical commands: Colon-separated command paths like
:MEASure:VOLTage? - Query commands: Commands ending with
? - Command parameters: Numeric values, strings, and enumerated values
- Command chaining: Multiple commands separated by semicolons
Installation
npm installBuilding
Generate the parser from the grammar:
npm run generateOr using tree-sitter CLI directly:
tree-sitter generateBuilding WASM
To build the WebAssembly version for use in VS Code extensions or web applications:
npm run build-wasmThis generates tree-sitter-scpi.wasm in the root directory. The WASM file is automatically built before publishing to npm.
Testing
Run the test suite:
npm testOr using tree-sitter CLI directly:
tree-sitter testGrammar Structure
Command Types
Common Commands
Common commands start with an asterisk (*) and may include a query marker (?):
*IDN?
*RST
*OPC?Hierarchical Commands
Hierarchical commands use colons (:) to separate command nodes. The leading colon is optional:
:MEASure:VOLTage?
MEASure:VOLTage?
:SYSTem:COMMunicate:SERial:BAUD 9600Parameters
Commands can include parameters separated by commas:
- Numbers:
5.0,-10,1.5e3,0.001 - Strings:
"192.168.1.100","AUTO" - Enumerations:
EXTernal,INTernal,AUTO
Example:
:MEASure:VOLTage:DC? 10,0.001
:SYSTem:COMMunicate:LAN:IPADdress "192.168.1.100"
:TRIGger:SOURce EXTernalCommand Chaining
Multiple commands can be chained on a single line using semicolons:
*IDN?;*RST
:MEASure:VOLTage?;:SOURce:VOLTage 5.0Example Usage
Node.js (Native Binding)
const Parser = require('tree-sitter');
const SCPI = require('tree-sitter-scpi');
const parser = new Parser();
parser.setLanguage(SCPI);
const sourceCode = `*IDN?
:MEASure:VOLTage?
:SOURce:VOLTage 5.0`;
const tree = parser.parse(sourceCode);
console.log(tree.rootNode.toString());WebAssembly (for VS Code Extensions and Web)
For VS Code extensions or web applications, use the WASM version with web-tree-sitter:
const Parser = require('web-tree-sitter');
async function initializeParser() {
await Parser.init();
const SCPI = await Parser.Language.load(
require.resolve('tree-sitter-scpi/tree-sitter-scpi.wasm')
);
const parser = new Parser();
parser.setLanguage(SCPI);
return parser;
}
// Usage
initializeParser().then(parser => {
const sourceCode = `*IDN?
:MEASure:VOLTage?
:SOURce:VOLTage 5.0`;
const tree = parser.parse(sourceCode);
console.log(tree.rootNode.toString());
});Note: Make sure to install web-tree-sitter as a dependency in your project:
npm install web-tree-sitterGrammar Rules
The grammar defines the following main rules:
program: Root node containing one or more commandscommand: Individual SCPI command (common or hierarchical)common_command: Asterisk-prefixed commandshierarchical_command: Colon-separated command pathcommand_path: Sequence of command nodes separated by colonscommand_node: Individual node in hierarchical pathquery_marker: Optional?at end of commandsparameter_list: Optional parameters following commandparameter: Individual parameter (number, string, or enumeration)number: Numeric values (integers, floats, scientific notation)string: Quoted string parametersenumeration: Enumerated values (keywords)
License
MIT
