fuhrmanator / GIFT-grammar-PEG.js

Development of PEG grammar to support GIFT (quiz) format
MIT License
37 stars 15 forks source link
gift grammar moodle peg quiz-questions

GIFT-grammar-PEG.js

Status

Node.js CI

Development of PEG grammar to parse GIFT (quiz question) file format. The goal is to find an intuitive and fun way to create quiz questions.

Initial hacking done using peggyjs.org/online. The GIFT.pegjs file goes on the left and the test GIFT goes on the right. Note that nothing is saved in this environment (you must copy-paste back to your own files).

GIFT parser (npm package gift-pegjs)

The PEG.js grammar in this repo is used to generate a parser for GIFT. You can see how it works in this online editor.

Software using the parser

For developers

Install

For simple web applications, copy the pegjs-gift.js file into your project.

For modern Javascript development, install the library using npm:

npm install gift-pegjs

Usage

Importing the NPM library in ES6+ Javascript (recommended):

import { parse } from "gift-pegjs";

const question = "What is the value of pi (to 3 decimal places)? {#3.141..3.142}."

const quiz = parse(question)

Importing the library for simple web applications:

import { parse } from "./pegjs-gift"; // Change path depending on where the file is copied.

const question = "What is the value of pi (to 3 decimal places)? {#3.141..3.142}."

const quiz = parse(question)

Importing the NPM library in CommonJS (for use in Node.js applications):

const parse = require("gift-pegjs");

const question = "What is the value of pi (to 3 decimal places)? {#3.141..3.142}."

const quiz = parse(question)

Using the optional Typescript types:

import { parse, GIFTQuestion } from "gift-pegjs";

const question: string = "What is the value of pi (to 3 decimal places)? {#3.141..3.142}."

const quiz: GIFTQuestion[] = parse(question)

Example

import { parse } from "gift-pegjs";

let sampleQuestion = `
Is this True? {T}

Mahatma Gandhi's birthday is an Indian holiday on {
~15th
~3rd
=2nd
} of October.

Since {
  ~495 AD
  =1066 AD
  ~1215 AD
  ~ 43 AD
} the town of Hastings England has been "famous with visitors".

What is the value of pi (to 3 decimal places)? {#3.141..3.142}.
`;

const quizQuestions = parse(sampleQuestion);
console.log(quizQuestions);

Tests

There are easily extendible tests from sample GIFT/JSON files in ./test/questions/.

To be able to run the tests, you must set up your environment:

GIFT format

General Import Format Technology (GIFT) is described at several places, but it's hard to know what the definitive source is.

This table is not sufficient to understand everything. Please see Moodle's documentation and this reference for many more details.

Symbols Use
// text Comment until end of line (optional)
::title:: Question title (optional)
text Question text (becomes title if no title specified)
[format] The format of the following bit of text. Options are [html], [moodle], [plain] and [markdown]. The default is [moodle] for the question text, other parts of the question default to the format used for the question text.
{ Start answer(s) -- without any answers, text is a description of following questions
{T} or {F} True or False answer; also {TRUE} and {FALSE}
{ ... =right ... } Correct answer for multiple choice, (multiple answer? -- see page comments) or fill-in-the-blank
{ ... ~wrong ... } Incorrect answer for multiple choice or multiple answer
{ ... =item -> match ... } Answer for matching questions
#feedback text Answer feedback for preceding multiple, fill-in-the-blank, or numeric answers
####general feedback General feedback
{# Start numeric answer(s)
    answer:tolerance Numeric answer accepted within ± tolerance range
    low..high Lower and upper range values of accepted numeric answer
    =%n%answer:tolerance n percent credit for one of multiple numeric ranges within tolerance from answer
} End answer(s)
\character Backslash escapes the special meaning of ~, =, #, {, }, and :
\n Places a newline in question text -- blank lines delimit questions

Source for above table

Parser in PHP: https://github.com/moodle/moodle/blob/master/question/format/gift/format.php

Background information

See these discussions in the Moodle forums:

See this test file from Moodle's code base.

Class model for Moodle questions (quiz) format

The following UML class diagram is an interpretation of the XML format for Moodle quiz import/export. Note that the XML format is naive, e.g., the QUESTION tag is overloaded using TYPE= and as such can't easily be schema-tized. The interpretation below aims to facilitate the understanding of the content and relations. It's a domain model reverse-engineered from a data model.

UML class diagram (domain model) loosely based on XML structure

Railroad diagram of the grammar

Check out the railroad diagram for this project's PEG.

Testing the editor locally