openspg-schema-antlr4
v0.1.4
Published
OpenSPG Schema Mark Language Lexer and Parser by official ANTLR4 grammar
Downloads
121
Maintainers
Readme
OpenSPG Schema Mark Language Lexer and Parser, generated by official ANTLR4 grammar.
Change Log · Report Bug · Pull Request

Installation
npm install openspg-schema-antlr4It will be
pnpm/yarn add openspg-schema-antlr4if you use pnpm or yarn.
Usage
Language Parser
parse(code, [options]):parse()parses the provided code as an entire Schema source unit.options:tolerant:boolean, default isfalse. Iftrue, the parser will try to parse as much as possible, even if the input is invalid, and never throw an error.selector:function, default is(p) => p.sourceUnit(). If provided, the parser will only return the nodes that match the selector. It will be useful when you want to parse a specific node.
output:SyntaxNode, the root node of the AST.
// parse.mjs
import {parse} from 'openspg-schema-antlr4';
const code = `// Sample
namespace BaiKe
Person(人物): EntityType
properties:
desc(描述): Text
index: TextAndVector
semanticType(语义类型): SemanticConcept
synonyms(同义词): Person
officialName(标准名): Person
Transport(运输): EntityType
properties:
desc(描述): Text
index: TextAndVector
semanticType(语义类型): SemanticConcept
synonyms(同义词): Transport
officialName(标准名): Transport
Works(作品): EntityType
properties:
desc(描述): Text
index: TextAndVector
semanticType(语义类型): SemanticConcept
synonyms(同义词): Works
officialName(标准名): Works
Event(事件): EventType
properties:
subject(主体): Person
participants(参与者): Person
constraint: MultiValue
time(时间): Date
location(地点): GeographicLocation
abstract(摘要): Text
index: TextAndVector
type(事件类型): Text
index: Text
`;
const ast = parse(code, {tolerant: true, selector: (p) => p.sourceUnit()});
Tokenizer
tokenizer(code, [options]):tokenizer()parses the provided code as tokens.options:tolerant:boolean, default isfalse.
output:SyntaxToken[].
// tokenizer.mjs
import {tokenizer} from 'openspg-schema-antlr4';
const tokens = tokenizer(code, {tolerant: true});
Traverse AST
We can use it alongside the parser to traverse nodes.
import {parse, visit, serialize} from 'openspg-schema-antlr4';
const ast = parse(code);
// Use `visit` to traverse ast by enter/exit node type.
visit(ast, {
enter: ({node, parent}) => {
console.log(node.type, parent?.type); // print node type
},
exit: () => {
}, // will call when exit node
Identifier: ({node: identifierNode}) => {
console.log(identifierNode.name); // print identifier name
},
exitContractDefinition: ({node: contractDefinitionNode}) => {
// will call when exit ContractDefinition node
}
});
// Use `serialize` to modify ast.
const newAST = serialize(ast, ({node}) => {
// do something
if (node.type === 'Identifier') {
return node.name;
}
return node;
})// traverse.mjs
import {parse, traverse} from 'openspg-schema-antlr4';
const ast = parse(code);
const newAST = traverse(ast, (path) => {
// path.path => `SourceUnit.Namespace` ...
// path.node => current node
// path.parentPath => parent node path
// path.depth => current node depth
// path.stop(); => stop traverse
// path.rewrite({...}); => rewrite current node
// path.matches({ type: 'xxx' }); => check if current node matches the given filter
// return () => {}; => will call when exit node
});Low-level API
Not recommended, but you can use it if you want.
import {SchemaLexer, SchemaParser, CharStream, CommonTokenStream} from 'openspg-schema-antlr4';
const code = `...`; // code here
const input = CharStream.fromString(code);
const lexer = new SolidityLexer(input);
const tokens = new CommonTokenStream(lexer);
const parser = new SolidityParser(tokens);
const parseTree = parser.sourceUnit();
// do something with parseTree