- MQL5 88.3%
- MQL4 11.7%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| Src | ||
| BasesParserSLan.mqproj | ||
| dependencies.json | ||
| LICENSE | ||
| README.md | ||
Base repository for parsers of structured languages (JSON, YAML, TOML, or custom formats) in MQL5.
Provides an abstract parser, a read-only node/navigation layer, a mutable DOM,
and a small query VM — all shared infrastructure that concrete parsers (
like JsonParserByLeo) extend rather than reimplement.
Main Features
This repository does not parse any format by itself. It provides the building blocks a concrete "structured language" parser is built from, and every layer is meant to be extended, not used directly.
- Abstract parser base (
CBaseStructuredLan,Main.mqh): holds the parsed data as a flat "cinta" (along[]tape), owns the perfect-hash tables used for O(1) key lookups, and handles saving/loading a fully compiled representation (tape + tables) to a binary cache file. A concrete parser extends this class and implementsCalcRegType(how to detect the file type) plus its own lexer/tokenizer that fills the tape (see JsonParserByLeo'sCJsonParser : public CBaseStructuredLan). - Read-only node/navigation layer (
CNodeSFLBase<TCtx, TType, TOut>,NodeBase.mqh): generic template for walking the tape without copying data —operator[]/Getby key hash,At/AtObjby index, array/object iterators, JIT perfect-hashing (an object is linearly scanned until it's accessed often enough, then a perfect-hash table is built for it on the fly), andQuery(path)for a JSON-Pointer-style path string. A concrete format defines its own node type on top, e.g.CJsonNode : CNodeSFLBase<CJsonParser, ENUM_JSON_VTYPE, CJsonNode>. - Mutable DOM (
CDomNodeBase+CDomNodeManager,DomBaseHeader.mqh/DomDef.mqh/DomImp.mqh): a fully independent, writable tree — arenas with free-lists for strings, array nodes and objects (objects are backed byCHashMapFastfrom FastCollectionsByLeo). Any read-only node can be materialized into this DOM viaToDom()/SerializeToDom(), which is how you turn a parsed-but-frozen document into something you can mutate, rebuild, or hand off between formats. - DOM pointer resolver (
CDomNodePointer<TCtx>,Pointer.mqh): resolves JSON-Pointer-style paths (/a/b/0) directly over the mutable DOM, independent of the read-only node layer's ownQuery. - Query VM (
Query/): a small register-based bytecode VM plus its own assembly-like compiler, for expressing JSONPath-style queries (filters, iteration, math, comparisons, function calls, slicing) that run directly over the DOM.CSLDomQueryCompilercompiles a.jsonpasmtext (or a raw bytecode cache) into bytecode;CSLDomQueryVm : public CSLDomQueryCompilerexecutes it viaRun(). Supports named jump labels, registers,SAVE/SAVE_NO_RESET, arithmetic and bitwise ops,COMPARE_EQ/MENOR/MENOR_EQ/IS, user-registered native functions (SetFunction), array slicing (Python-style start/end/step) andCOPY_ALL/APPENDto collect results.
Usage example — extending the base parser (pattern used by JsonParserByLeo)
// 1. Extend the abstract parser
class CJsonParser : public TSN::CBaseStructuredLan
{
private:
// Aqui podrian ir pilas, o lo que use tu lengauje depende quizas lo haces recusivo o con pilas...
int CalcRegType(const string& file_name) override final; // detecta .json / .jsonasm / cache
public:
bool Parse(); // tu lexer propio, llena m_cinta
};
// 2. Extend the node layer for typed, read-only navigation
// <Tu clase de pasing, Tu Enumeracino de los tipos disponibles, y El mismo nombre del nodo>
struct CJsonNode : TSN::CNodeSFLBase<CJsonParser, ENUM_JSON_VTYPE, CJsonNode>
{
CJsonNode operator[](const string& key); // usa perfect hash / linear probing
};
// 3. Uso (con JsonParserByLeo)
CJsonParser parser;
parser.AssingFile("config.json", false);
parser.Parse();
CJsonNode root = parser.GetRoot();
string val = root["precios"]["libro1"].ToString();
Usage example — JSONPath-style query (.jsonpasm)
; $.libros[?(@.comando * 20 < @.valor && @.sumando.size()].sumando[*]
;--- Example in ASM DOM
; Registers
%reg 0 = 20
%reg 1 = 1
; Code
INS_ACCESO_KEY_F K"libros"
INS_INICIAR_ITERACION 1
@Iteracion:
INS_ITER_ASSING 'Final'
INS_ACCESO_KEY 'Iteracion' K"comando"
INS_SAVE 256 'Iteracion'
INS_M_MUL 256 0 256 'Iteracion'
INS_ACCESO_KEY 'Iteracion' K"valor"
INS_SAVE 257 'Iteracion'
INS_COMPARE_MENOR 256 257 1 'Iteracion'
INS_ACCESO_KEY 'Iteracion' K"sumando"
INS_SAVE_NO_RESET 256 'Iteracion'
INS_CALL 256 1 'size'
INS_COMPARE_EQ 257 1 1 'Iteracion'
INS_COMPARE_IS 258 'Iteracion' 1
INS_COPY_ALL 'Iteracion'
INS_JUMP 'Iteracion'
@Final:
INS_SALIDA
Compiled with
CSLDomQueryCompiler::CompileAsm()and executed against aCDomNodeBase*tree viaCSLDomQueryVm::Run().
Extending to a new format (TOML, YAML, custom)
The intended path for a new "structured language" is the same one JsonParserByLeo follows — see its JsonParser.mqh, JsonNode.mqh and JsonPointer.mqh for a concrete reference:
- Extend
CBaseStructuredLanwith your own tokenizer/lexer that fillsm_cintausing this repo's tape layout (seeDef.mqhfor the bit layout of KEY/OBJ/ARR/INT/FLT/BOL/STR slots). - Extend
CNodeSFLBase<TCtx, TType, TOut>with a typed node struct for your format (key access, array access,ToString/ToInt/etc., followingCJsonNodeas a template). - Reuse
CDomNodeBase/CDomNodeManageras-is for the mutable DOM — no changes needed there. - Reuse
CDomNodePointer<TCtx>as-is for pointer-style path resolution over the DOM. - Reuse the Query VM (
Query/) as-is if you want JSONPath-style queries over your format's DOM.
Repository Structure
BasesParserSLan/
└── Src/ # Parser base, NodeBase, DOM, Pointer, and the Query VM (Query/)
Requirements
See dependencies.json for the full list.
- MetaTrader 5, build 5430+
Installation
cd "C:\Users\YOUR_USER\AppData\Roaming\MetaQuotes\Terminal\YOUR_ID\MQL5\Shared Projects"
tsndep install "https://forge.mql5.io/nique_372/BasesParserSLan.git"
Requires the tsndep package, available on PyPI. It automatically downloads and installs all declared dependencies.
Quick Start
1. Include the layer you need:
#include <TSN\\BSFL\\NodeBase.mqh> // parser base + node layer
#include <TSN\\BSFL\\Dom.mqh> // + mutable DOM
#include <TSN\\BSFL\\Query.mqh> // + query VM
2. This repo alone does nothing — extend CBaseStructuredLan and CNodeSFLBase for your format, or depend on a repo that already did (e.g. JsonParserByLeo).
License
Read Full License By downloading or using this repository, you accept the license terms.
Contact
- Platform: MQL5 Community
- Profile: https://www.mql5.com/es/users/nique_372
- Articles: https://www.mql5.com/es/users/nique_372/publications