@isuckatbeingsocial/parse-cs
v1.0.9
Published
**ParseCS** is a C# parsing library written in pure JavaScript, featuring a basic plugin system. It requires no external dependencies beyond those used by Webpack.
Readme
ParseCS
ParseCS is a C# parsing library written in pure JavaScript, featuring a basic plugin system. It requires no external dependencies beyond those used by Webpack.
- Roughly supports C# versions between 1.0 and 3.0.
Release Notes
Version 1.0.3
- Safer parsing in critical areas, significantly reducing risk of infinite loops
- Full support for
enumdeclarations - Full support for C# modifiers on classes and enums
- Full support for
forloops - Added support for multi-variable declarations in a single statement, e.g.:
int i = 0, i2 = 0;
Version 1.0.4
- No changes, fixing README.md to display accurate information.
Version 1.0.5
- No changes, fixing README.md to display accurate information.
Version 1.0.6
Full support for
switchstatements, including:caselabelsdefaultlabelswhenconditions
Full support for
gotostatements, including:goto case <value>;goto default;goto <Label>;(Label support is planned for a future release)
Version 1.0.7
Minor Update
- Added support for label declarations (used with
goto <Label>;)MyLabel:
Version 1.0.8
Minor Update
- Fixed class parsing support and resolved infinite loop issues during member parsing.
- Unified modifier structure across class members, classes, and enums for more consistent AST output.
- Added validation to disallow
constandstaticmodifiers from coexisting on class members, matching C# language rules. - Removed old debug logs I forgot to take out
Version 1.0.9
- No changes, fixing README.md to display accurate information.
Installation Guide
Open a terminal.
Navigate to your project’s root directory:
cd path/to/your/projectInstall the package via npm:
npm install @isuckatbeingsocial/parse-csBuild the parser module:
cd ./node_modules/@isuckatbeingsocial/parse-cs npm install npm run build cd ../../../Require ParseCS in your project:
const ParseCS = require('@isuckatbeingsocial/parse-cs'); const parser = new ParseCS();
Usage
Parsing C# Code
const result = parser.parse(`
public class MyClass
{
public int myField;
public void MyMethod()
{
// Parsed code goes here.
}
}
`);
const ast = result.result;
// Alternatively, to access lexer and parser:
const { lexer, parser: innerParser, result: ast2 } = parser.parse('...');Writing Plugins
const plugins = parser.plugin;
const MyParseCSPlugin = function() {
return {
extensions: {
my_extension: {
lex: (Lexer) => {
// Called for every new lexer using this plugin.
Lexer.tokenTypes.push({
ignore: true, // If true, the token is not added to the final token list.
regex: 'your-regex-here',
type: 'MY_TOKEN_TYPE'
});
// Register custom token type
Lexer.constructor.exports.TokenTypes.mytoken = 'MY_TOKEN_TYPE';
},
parse: (Parser, TokenTypes) => {
Parser.ParseMyToken = function() {
this.index(1); // Skip the token
return { type: 'MyTokenExpression', value: null };
};
Parser.expressionExtensions.push(function() {
const isMyToken = this.expect(TokenTypes.mytoken, undefined, {
throw: false,
errorMessage: null
});
return isMyToken ? Parser.ParseMyToken() : false;
});
}
}
}
};
};
// Register the plugin
plugins.instantiate(MyParseCSPlugin);You can now parse code with your custom plugin logic enabled.
