I am currently working on a CodeMirror mode for React JSX and have run into a problem.
High level, I am jumping between the existing Javascript and XML (HTML) modes as appropriate, pushing mode and state info onto a state stack since each mode can be nested infinitely deep.
My problem presents itself when I try to parse a JSX element attribute value with the XML mode. For a contrived example:
return (<div foo={return 2 > 3;}></div>);
In theory the process is to start in Javascript mode, push to XML mode on the first <
, parse with XML mode until just after the =
, eat the {
, push to a new Javascript mode and state, parse with Javascript mode until the matching }
, pop Javascript mode, parse with current XML mode and state, etc.
The problem is that the XML mode has several different states stored in state.state
and I can’t figure out how to force the XML mode into one of those states after I finish parsing the Javascript between the {}
's. It should be set to function attrContinuedState()
and it isn’t. Nor can I copy the state ahead of time so I can store it on a stack.
Or, to put it slightly less technically, I can’t figure out where to override the XML mode implementation to parse this correctly. Should I rewriting the entire function inTag()
tokenizer? I think one of the problems is all of the module-level functions, the tokenizers and the states, that have no public API as far as I can tell.
Sorry, I can’t explain this problem more clearly, but it is rather complex for me and I am just looking for some high level ideas.
Thanks,
Lance.