-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
allow sizing commands inside optional groups #885
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -165,7 +165,7 @@ export default class Parser { | |
| if (breakOnInfix && functions[lex.text] && functions[lex.text].infix) { | ||
| break; | ||
| } | ||
| const atom = this.parseAtom(); | ||
| const atom = this.parseAtom(breakOnTokenText); | ||
| if (!atom) { | ||
| if (!this.settings.throwOnError && lex.text[0] === "\\") { | ||
| const errorNode = this.handleUnsupportedCmd(); | ||
|
|
@@ -306,12 +306,13 @@ export default class Parser { | |
| /** | ||
| * Parses a group with optional super/subscripts. | ||
| * | ||
| * @param {"]" | "}"} breakOnTokenText - character to stop parsing the group on. | ||
| * @return {?ParseNode} | ||
| */ | ||
| parseAtom() { | ||
| parseAtom(breakOnTokenText) { | ||
| // The body of an atom is an implicit group, so that things like | ||
| // \left(x\right)^2 work correctly. | ||
| const base = this.parseImplicitGroup(); | ||
| const base = this.parseImplicitGroup(breakOnTokenText); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be worth adding a comment somewhere as to why it's okay not to pass this parameter down to other methods below. The reason seems to be subtle: as I'm also fine with completely deferring this if we intend to formalize the various non-obvious invariants in this file some point to aid comprehensibility so that they aren't violated in the future.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll add a comment. |
||
|
|
||
| // In text mode, we don't have superscripts or subscripts | ||
| if (this.mode === "text") { | ||
|
|
@@ -423,9 +424,10 @@ export default class Parser { | |
| * small text {\Large large text} small text again | ||
| * It is also used for \left and \right to get the correct grouping. | ||
| * | ||
| * @param {"]" | "}"} breakOnTokenText - character to stop parsing the group on. | ||
| * @return {?ParseNode} | ||
| */ | ||
| parseImplicitGroup() { | ||
| parseImplicitGroup(breakOnTokenText) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, please. |
||
| const start = this.parseSymbol(); | ||
|
|
||
| if (start == null) { | ||
|
|
@@ -484,7 +486,7 @@ export default class Parser { | |
| } else if (utils.contains(Parser.sizeFuncs, func)) { | ||
| // If we see a sizing function, parse out the implicit body | ||
| this.consumeSpaces(); | ||
| const body = this.parseExpression(false); | ||
| const body = this.parseExpression(false, breakOnTokenText); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does this only apply to this block and not to the rest below:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question. I was fixing only the issue, but I'm sure there are places where KA also uses |
||
| return new ParseNode("sizing", { | ||
| // Figure out what size to use based on the list of functions above | ||
| size: utils.indexOf(Parser.sizeFuncs, func) + 1, | ||
|
|
@@ -493,7 +495,7 @@ export default class Parser { | |
| } else if (utils.contains(Parser.styleFuncs, func)) { | ||
| // If we see a styling function, parse out the implicit body | ||
| this.consumeSpaces(); | ||
| const body = this.parseExpression(true); | ||
| const body = this.parseExpression(true, breakOnTokenText); | ||
| return new ParseNode("styling", { | ||
| // Figure out what style to use by pulling out the style from | ||
| // the function name | ||
|
|
@@ -504,7 +506,7 @@ export default class Parser { | |
| const style = Parser.oldFontFuncs[func]; | ||
| // If we see an old font function, parse out the implicit body | ||
| this.consumeSpaces(); | ||
| const body = this.parseExpression(true); | ||
| const body = this.parseExpression(true, breakOnTokenText); | ||
| if (style.slice(0, 4) === 'text') { | ||
| return new ParseNode("text", { | ||
| style: style, | ||
|
|
@@ -522,7 +524,7 @@ export default class Parser { | |
| if (!color) { | ||
| throw new ParseError("\\color not followed by color"); | ||
| } | ||
| const body = this.parseExpression(true); | ||
| const body = this.parseExpression(true, breakOnTokenText); | ||
| return new ParseNode("color", { | ||
| type: "color", | ||
| color: color.result.value, | ||
|
|
@@ -834,7 +836,7 @@ export default class Parser { | |
| if (this.nextToken.text === (optional ? "[" : "{")) { | ||
| // If we get a brace, parse an expression | ||
| this.consume(); | ||
| const expression = this.parseExpression(false, optional ? "]" : null); | ||
| const expression = this.parseExpression(false, optional ? "]" : "}"); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why this was
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parsing always ends on
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It feels nice to have symmetry the |
||
| const lastToken = this.nextToken; | ||
| // Make sure we get a close brace | ||
| this.expect(optional ? "]" : "}"); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`An implicit group parser within optional groups should work style commands \\sqrt[\\textstyle 3]{x} 1`] = ` | ||
| [ | ||
| { | ||
| "type": "sqrt", | ||
| "mode": "math", | ||
| "value": { | ||
| "type": "sqrt", | ||
| "body": { | ||
| "type": "ordgroup", | ||
| "mode": "math", | ||
| "value": [ | ||
| { | ||
| "type": "mathord", | ||
| "mode": "math", | ||
| "value": "x" | ||
| } | ||
| ] | ||
| }, | ||
| "index": { | ||
| "type": "ordgroup", | ||
| "mode": "math", | ||
| "value": [ | ||
| { | ||
| "type": "styling", | ||
| "mode": "math", | ||
| "value": { | ||
| "style": "text", | ||
| "value": [ | ||
| { | ||
| "type": "textord", | ||
| "mode": "math", | ||
| "value": "3" | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| `; | ||
|
|
||
| exports[`An implicit group parser within optional groups should work with \\color: \\sqrt[\\color{red} 3]{x} 1`] = ` | ||
| [ | ||
| { | ||
| "type": "sqrt", | ||
| "mode": "math", | ||
| "value": { | ||
| "type": "sqrt", | ||
| "body": { | ||
| "type": "ordgroup", | ||
| "mode": "math", | ||
| "value": [ | ||
| { | ||
| "type": "mathord", | ||
| "mode": "math", | ||
| "value": "x" | ||
| } | ||
| ] | ||
| }, | ||
| "index": { | ||
| "type": "ordgroup", | ||
| "mode": "math", | ||
| "value": [ | ||
| { | ||
| "type": "color", | ||
| "mode": "math", | ||
| "value": { | ||
| "type": "color", | ||
| "color": "red", | ||
| "value": [ | ||
| { | ||
| "type": "textord", | ||
| "mode": "math", | ||
| "value": "3" | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| `; | ||
|
|
||
| exports[`An implicit group parser within optional groups should work with sizing commands: \\sqrt[\\small 3]{x} 1`] = ` | ||
| [ | ||
| { | ||
| "type": "sqrt", | ||
| "mode": "math", | ||
| "value": { | ||
| "type": "sqrt", | ||
| "body": { | ||
| "type": "ordgroup", | ||
| "mode": "math", | ||
| "value": [ | ||
| { | ||
| "type": "mathord", | ||
| "mode": "math", | ||
| "value": "x" | ||
| } | ||
| ] | ||
| }, | ||
| "index": { | ||
| "type": "ordgroup", | ||
| "mode": "math", | ||
| "value": [ | ||
| { | ||
| "type": "sizing", | ||
| "mode": "math", | ||
| "value": { | ||
| "size": 5, | ||
| "value": [ | ||
| { | ||
| "type": "textord", | ||
| "mode": "math", | ||
| "value": "3" | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| `; | ||
|
|
||
| exports[`An implicit group parser within optional groups should work wwith old font functions: \\sqrt[\\tt 3]{x} 1`] = ` | ||
| [ | ||
| { | ||
| "type": "sqrt", | ||
| "mode": "math", | ||
| "value": { | ||
| "type": "sqrt", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (optional) I don't know whether an arbitrary strict order is enforced here, but if not, moving this up would make it much easier to see which level it is in.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can set up a custom formatter for the snapshots to always put |
||
| "body": { | ||
| "type": "ordgroup", | ||
| "mode": "math", | ||
| "value": [ | ||
| { | ||
| "type": "mathord", | ||
| "mode": "math", | ||
| "value": "x" | ||
| } | ||
| ] | ||
| }, | ||
| "index": { | ||
| "type": "ordgroup", | ||
| "mode": "math", | ||
| "value": [ | ||
| { | ||
| "type": "font", | ||
| "mode": "math", | ||
| "value": { | ||
| "body": { | ||
| "type": "ordgroup", | ||
| "mode": "math", | ||
| "value": [ | ||
| { | ||
| "type": "textord", | ||
| "mode": "math", | ||
| "value": "3" | ||
| } | ||
| ] | ||
| }, | ||
| "font": "mathtt" | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| `; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Mind adding a
@paramdocumenting the type of the new parameter?