Base repository for parsers of structured languages ​​(json, yaml, etc.)
  • MQL5 88.3%
  • MQL4 11.7%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Nique_372 2be9fb400a
2026-08-04 11:50:35 -05:00
Src 2026-08-04 11:50:35 -05:00
BasesParserSLan.mqproj Generated by MQL5 Wizard 2026-07-18 10:46:45 -05:00
dependencies.json new files added 2026-07-19 12:14:17 -05:00
LICENSE Añadir LICENSE 2026-07-19 12:43:49 +00:00
README.md 2026-08-01 18:06:13 -05:00

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" (a long[] 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 implements CalcRegType (how to detect the file type) plus its own lexer/tokenizer that fills the tape (see JsonParserByLeo's CJsonParser : public CBaseStructuredLan).
  • Read-only node/navigation layer (CNodeSFLBase<TCtx, TType, TOut>, NodeBase.mqh): generic template for walking the tape without copying data — operator[]/Get by key hash, At/AtObj by 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), and Query(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 by CHashMapFast from FastCollectionsByLeo). Any read-only node can be materialized into this DOM via ToDom() / 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 own Query.
  • 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. CSLDomQueryCompiler compiles a .jsonpasm text (or a raw bytecode cache) into bytecode; CSLDomQueryVm : public CSLDomQueryCompiler executes it via Run(). 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) and COPY_ALL/APPEND to 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 a CDomNodeBase* tree via CSLDomQueryVm::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:

  1. Extend CBaseStructuredLan with your own tokenizer/lexer that fills m_cinta using this repo's tape layout (see Def.mqh for the bit layout of KEY/OBJ/ARR/INT/FLT/BOL/STR slots).
  2. Extend CNodeSFLBase<TCtx, TType, TOut> with a typed node struct for your format (key access, array access, ToString/ToInt/etc., following CJsonNode as a template).
  3. Reuse CDomNodeBase / CDomNodeManager as-is for the mutable DOM — no changes needed there.
  4. Reuse CDomNodePointer<TCtx> as-is for pointer-style path resolution over the DOM.
  5. 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