Skip to content

Commit

Permalink
Add a textFrom to option blocks.
Browse files Browse the repository at this point in the history
This fixes another position issue.

Also, don't add a block balance "to" position to a block's "to" whose
stack it is pushed onto.

Remove the "breakInside" option that is not used by this parser.
  • Loading branch information
drgrice1 committed Oct 19, 2024
1 parent f5ba1cd commit 3a7130e
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openwebwork/codemirror-lang-pg",
"version": "0.0.1-beta.7",
"version": "0.0.1-beta.8",
"description": "PG language support for CodeMirror",
"author": "The WeBWorK Project",
"license": "MIT",
Expand Down
15 changes: 6 additions & 9 deletions src/pgml-parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ interface BlockDefinition {
allowStar?: boolean;
allowTriStar?: boolean;
balance?: RegExp;
breakInside?: boolean;
cancelNL?: boolean;
cancelPar?: boolean;
cancelUnbalanced?: boolean;
Expand Down Expand Up @@ -300,9 +299,7 @@ export class PGMLParse {
if (this.block) this.block.to += 1;
this.ignoreNL = 0;
} else {
const top = this.block?.topItem();
if (top && top instanceof Block && top.breakInside) top.pushText(token);
else this.Text(token);
this.Text(token);
this.ignoreNL = 1;
}
this.atLineStart = 1;
Expand All @@ -315,9 +312,7 @@ export class PGMLParse {
this.Item('forced', token, { noIndent: 1 });
this.indent = 0;
} else {
const top = this.block?.topItem() as Block;
if (top.breakInside) top.pushItem(new Item('break', this.pos, { token }));
else this.Item('break', token, { noIndent: 1 });
this.Item('break', token, { noIndent: 1 });
}
this.atLineStart = this.ignoreNL = 1;
this.actualIndent = 0;
Expand Down Expand Up @@ -651,6 +646,8 @@ export class PGMLParse {
const optionBlock = this.block?.popItem();
const block = this.block?.topItem();
if (!(block instanceof Item) || !(optionBlock instanceof Item)) return;
const firstItem = optionBlock.stack?.at(0);
optionBlock.textFrom = firstItem instanceof Item ? firstItem.from : optionBlock.from;
optionBlock.text = this.stackText(optionBlock.stack);
delete optionBlock.stack;
block.pushOption(optionBlock);
Expand Down Expand Up @@ -755,7 +752,6 @@ export class Item implements BlockDefinition {
allowStar?: boolean;
allowTriStar?: boolean;
balance?: RegExp;
breakInside?: boolean;
cancelNL?: boolean;
cancelPar?: boolean;
cancelUnbalanced?: boolean;
Expand Down Expand Up @@ -868,9 +864,10 @@ export class Block extends Item {

pushItem(...items: (string | Item)[]) {
this.to = items.reduce((max, i) => {
if (i instanceof Item && i.type === 'balance') return max;
const to = i instanceof Item ? i.to : i.length;
return to > max ? to : max;
}, 0);
}, this.to);
this.stack?.push(...items);
}

Expand Down
3 changes: 2 additions & 1 deletion src/pgml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ const pgmlFormat = (block: Item, offset: number): Element<Type>[] => {
return [elt(Type.Tag, block.from + offset, to, children), ...options];
} else if (block.type === 'options') {
children.unshift(elt(Type.OptionMark, block.from + offset, block.from + 1 + offset));
if (block.text) children.push(new TreeElement(pgPerlParser.parse(block.text), block.from + 1 + offset));
if (block.text)
children.push(new TreeElement(pgPerlParser.parse(block.text), (block.textFrom ?? block.from + 1) + offset));
children.push(elt(Type.OptionMark, block.to - 1 + offset, block.to + offset));
return [elt(Type.Option, block.from + offset, block.to + offset, children)];
} else if (block.type === 'bold' || block.type === 'italic') {
Expand Down
161 changes: 154 additions & 7 deletions test/pgml-answers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,33 @@ stack: [

BEGIN_PGML
[_]{$a}{15}
[_]{
$sum->cmp(mathQuillOpts => { autoCommands => 'pi sqrt root vert inf union abs Delta ' })
}{30}
END_PGML

==>

{
"name": "Program",
"from": 0,
"to": 31,
"to": 132,
"children": [
{
"name": "PGMLBlock",
"from": 0,
"to": 31,
"to": 132,
"children": [
{ "name": "BeginPG", "from": 0, "to": 10 },
{
"name": "PGMLContent",
"from": 10,
"to": 23,
"to": 124,
"children": [
{
"name": "Paragraph",
"from": 10,
"to": 23,
"to": 124,
"children": [
{ "name": "AnswerRule", "from": 11, "to": 14 },
{
Expand Down Expand Up @@ -147,12 +150,114 @@ END_PGML
},
{ "name": "OptionMark", "from": 21, "to": 22 }
]
},
{ "name": "AnswerRule", "from": 23, "to": 26 },
{
"name": "Option",
"from": 26,
"to": 119,
"children": [
{ "name": "OptionMark", "from": 26, "to": 27 },
{
"name": "Program",
"from": 28,
"to": 118,
"children": [
{
"name": "ExpressionStatement",
"from": 29,
"to": 117,
"children": [
{
"name": "MethodInvocation",
"from": 29,
"to": 117,
"children": [
{ "name": "ScalarVariable", "from": 29, "to": 33 },
{ "name": "ArrowOperator", "from": 33, "to": 35 },
{
"name": "FunctionName",
"from": 35,
"to": 38,
"children": [
{ "name": "Identifier", "from": 35, "to": 38 }
]
},
{
"name": "ParenthesizedArguments",
"from": 38,
"to": 117,
"children": [
{ "name": "(", "from": 38, "to": 39 },
{
"name": "Pair",
"from": 39,
"to": 116,
"children": [
{ "name": "Identifier", "from": 39, "to": 52 },
{ "name": "FatComma", "from": 53, "to": 55 },
{
"name": "HashRef",
"from": 56,
"to": 116,
"children": [
{ "name": "{", "from": 56, "to": 57 },
{
"name": "Pair",
"from": 58,
"to": 114,
"children": [
{ "name": "Identifier", "from": 58, "to": 70 },
{ "name": "FatComma", "from": 71, "to": 73 },
{ "name": "StringSingleQuoted", "from": 74, "to": 114 }
]
},
{ "name": "}", "from": 115, "to": 116 }
]
}
]
},
{ "name": ")", "from": 116, "to": 117 }
]
}
]
}
]
}
]
},
{ "name": "OptionMark", "from": 118, "to": 119 }
]
},
{
"name": "Option",
"from": 119,
"to": 123,
"children": [
{ "name": "OptionMark", "from": 119, "to": 120 },
{
"name": "Program",
"from": 120,
"to": 122,
"children": [
{
"name": "ExpressionStatement",
"from": 120,
"to": 122,
"children": [
{ "name": "Integer", "from": 120, "to": 122 }
]
}
]
},
{ "name": "OptionMark", "from": 122, "to": 123 }
]
}
]
}
]
},
{ "name": "EndPG", "from": 23, "to": 31 }
{ "name": "EndPG", "from": 124, "to": 132 }
]
}
]
Expand All @@ -162,14 +267,14 @@ END_PGML

from: '0'
parseAll: 'true'
to: '12'
to: '113'
type: 'root'
stack: [
[ # 0
combine: { indent: 'indent', list: { indent: '1' }, par: 'true' }
from: '0'
indent: '0'
to: '12'
to: '113'
type: 'indent'
stack: [
[ # 0
Expand All @@ -179,6 +284,7 @@ stack: [
from: '3'
terminator: '}'
text: '$a'
textFrom: '4'
to: '7'
token: '{'
type: 'options'
Expand All @@ -187,6 +293,7 @@ stack: [
from: '7'
terminator: '}'
text: '15'
textFrom: '8'
to: '11'
token: '{'
type: 'options'
Expand All @@ -204,6 +311,40 @@ stack: [
type: 'text'
stack: ['\n']
]
[ # 2
from: '12'
optionStack: [
{
from: '15'
terminator: '}'
text: '\t$sum->cmp(mathQuillOpts => { autoCommands => 'pi sqrt root vert inf union abs Delta ' })\n'
textFrom: '17'
to: '108'
token: '{'
type: 'options'
},
{
from: '108'
terminator: '}'
text: '30'
textFrom: '109'
to: '112'
token: '{'
type: 'options'
}
]
options: ['answer', 'width', 'name', 'cmp_options']
to: '112'
token: '[_]'
type: 'answer'
]
[ # 3
combine: { text: 'type' }
from: '112'
to: '113'
type: 'text'
stack: ['\n']
]
]
]
]
Expand Down Expand Up @@ -318,6 +459,7 @@ stack: [
from: '4'
terminator: '}'
text: '$matrix'
textFrom: '5'
to: '13'
token: '{'
type: 'options'
Expand All @@ -326,6 +468,7 @@ stack: [
from: '13'
terminator: '}'
text: '10'
textFrom: '14'
to: '17'
token: '{'
type: 'options'
Expand Down Expand Up @@ -515,6 +658,7 @@ stack: [
from: '3'
terminator: '}'
text: ' answer => $pi, name => NEW_ANS_NAME(), width => 10, cmp_options => { tolerance => 0.00001 } '
textFrom: '4'
to: '98'
token: '{'
type: 'options'
Expand Down Expand Up @@ -696,6 +840,7 @@ stack: [
from: '3'
terminator: '}'
text: '$pi'
textFrom: '4'
to: '8'
token: '{'
type: 'options'
Expand All @@ -704,6 +849,7 @@ stack: [
from: '8'
terminator: '}'
text: '10'
textFrom: '9'
to: '12'
token: '{'
type: 'options'
Expand All @@ -712,6 +858,7 @@ stack: [
from: '12'
terminator: '}'
text: ' cmp_options => { tolerance => 0.00001 } '
textFrom: '13'
to: '55'
token: '{'
type: 'options'
Expand Down
Loading

0 comments on commit 3a7130e

Please sign in to comment.