A parser for Moodle's GIFT format designed for error checking in IDE's. It is the library that powers the VSCode GIFT Language Extension.
This project builds on fuhrmanator's GIFT grammar, adding heuristic based error recovery and incremental parsing to deliver better error reports for end users.
npm install gift-parser-ide
There are two parsers available:
- Class-based incremental parser: This parser diffs the changes in the text from the previous parse and only parses the changes. This improves the speed of parsing drastically for repeated parsing, making it useful for IDE's.
- Function-based one-shot parser: Parses the whole input text everytime a new text is given.
ES6+
import GIFTParser from "gift-parser-ide"; // Default: Class-based Incremental Parser
import { parser } from "gift-parser-ide"; // Function-based One-Shot Parser
Node/CommonJS
var Parser = require("gift-parser-ide").default; // Default: Class-based Incremental Parser
var parser = require("gift-parser-ide").parser; // Function-based One-Shot Parser
var text = "::Title:: Gift Question {}";
// Class
var Parser = new GIFTParser();
Parser.update(text); //Updates the parser with new text
var output = Parser.result(); // Returns GIFTResult[]
// Function
var output = parser.parse(text); // Returns GIFTResult[]
- Updates the GIFTParser with new text. To return a value, use
.result()
to get a full result or.parseOnly()
and.errorOnly()
to get only the parsed output or error output respectively.
- Returns a
GIFTResult[]
which includes both parsed sections of GIFT and sections with errors.
- Returns the parsed output of a GIFT text. Does not throw an error if a section cannot be parsed.
- Returns all errors found within a GIFT text.
- Parses a GIFT text and returns both parsed sections of GIFT and sections with errors. Functionally equivalent to
GIFTParser.update(text).result()
without keeping previous parses.
- Returns the parsed output of a GIFT text. Does not throw an error if a section cannot be parsed. Functionally equivalent to
GIFTParser.update(text).parseOnly()
without keeping previous parses.
- Returns all errors found within a GIFT text. Functionally equivalent to
GIFTParser.update(text).errorOnly()
without keeping previous parses.
- Wraps the raw GIFT parser. Mainly used as a utility class.
MIT