-
Notifications
You must be signed in to change notification settings - Fork 0
/
gift.pegjs
179 lines (155 loc) · 3.87 KB
/
gift.pegjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
{
function buildQuestion(qtype, title, prefix, answer, suffix) {
var result = {
type: qtype
};
if (title) {
result.title = title;
}
if (qtype == "I") {
result.text = prefix;
} else {
result.question = prefix;
if (suffix) {
result.suffix = suffix;
}
}
if (answer) {
result.answer = answer;
delete answer.type;
}
return result;
}
function cleanupText(t) {
return t.replace(/[ \t\r\n]+/g, " ").trim();
}
}
QuestionList
= _? __* first:Question rest:(__ __+ Question)* __* {
var questions = [first].concat(rest.map(function(e) {
return e[2];
}));
return questions;
}
Question
= t:("::" TitleText "::")? __? q:QuestionText "{" _? a:FillInAnswer _? "}" s:QuestionText {
return buildQuestion(a.type, t ? t[1] : null, q, a, s);
}
/ t:("::" TitleText "::")? __? q:QuestionText "{" _? a:EndAnswer _? "}" {
return buildQuestion(a.type, t ? t[1] : null, q, a);
}
/ t:("::" TitleText "::")? __? q:QuestionText "{" _? "}" {
return buildQuestion("E", t ? t[1] : null, q);
}
/ t:("::" TitleText "::")? __? q:QuestionText {
return buildQuestion("I", t ? t[1] : null, q);
}
FillInAnswer
= BooleanAnswer
/ NumericAnswer
/ a:MultipleChoiceAnswer+ { return { type: "MC", choices: a }; }
EndAnswer
= a:MatchingAnswer+ { return { type: "M", pairs: a }; }
/ FillInAnswer
BooleanAnswer "boolean answer"
= answer:BooleanToken feedback:(_? "#" AnswerText)? {
var result = {
type: "TF",
correct: answer
};
if (feedback) {
result.feedback = feedback[2];
}
return result;
}
BooleanToken "boolean"
= "T"i "RUE"i? { return true; }
/ "F"i "ALSE"i? { return false; }
NumericSingleAnswer
= answer:Number !".." tolerance:(_? ":" Number)? {
return {
low: answer - (tolerance ? tolerance[2] : 0),
high: answer + (tolerance ? tolerance[2] : 0)
};
}
/ low:Number ".." high:Number {
return {
low: low,
high: high
};
}
NumericMultipleChoiceAnswer
= "=" weight:Weight? answer:NumericSingleAnswer feedback:(_? "#" AnswerText)? {
var result = {
low: answer.low,
high: answer.high
};
if (feedback) {
result.feedback = feedback[2];
}
if (weight) {
result.weight = weight;
};
return result;
}
NumericAnswer "numeric answer"
= "#" _? a:NumericSingleAnswer {
return {
type: "N",
low: a.low,
high: a.high
};
}
/ "#" a:(_? NumericMultipleChoiceAnswer)+ {
return {
type: "NMC",
choices: a.map(function(e) { return e[1]; })
};
}
Number "number"
= "-"? [0-9]+ ("." [0-9]+)? { return parseFloat(text()); }
MatchingAnswer "matching answer"
= "=" left:AnswerText "->" right:AnswerText {
return {
left: left,
right: right
}
}
MultipleChoiceAnswer "multiple choice answer"
= op:ChoiceOperator weight:Weight? answer:AnswerText feedback:(_? "#" AnswerText)? {
var result = {
correct: op,
text: answer
};
if (feedback) {
result.feedback = feedback[2];
}
if (weight) {
result.weight = weight;
};
return result;
}
ChoiceOperator
= "=" { return true; }
/ "~" { return false; }
Weight "percent credit"
= "%" weight:$("-"? [0-9.]+) "%" { return parseInt(weight); }
AnswerText "answer text"
= (!"->" [^~=#}])+ { return cleanupText(text()); }
QuestionText "question text"
= (!("{" / NewLine HorizontalWhitespace* NewLine) .)+ { return cleanupText(text()); }
TitleText "title text"
= (!"::" .)+ { return cleanupText(text()); }
Comment "comment"
= "//" (!(NewLine / EOF) .)* (NewLine / EOF)
NewLine "newline"
= "\r"? "\n"
EOF "end of input"
= !.
HorizontalWhitespace "whitespace"
= [ \t]
_ "whitespace"
= [ \t\r\n]+
__
= HorizontalWhitespace* NewLine HorizontalWhitespace*
/ HorizontalWhitespace* Comment HorizontalWhitespace*