Skip to content

Commit

Permalink
Rename parser stages bind -> define, preprocess -> init and finish ->…
Browse files Browse the repository at this point in the history
… resolve.
  • Loading branch information
jjrv committed Dec 19, 2015
1 parent 0dc535b commit fd3c6a5
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 22 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ The static `mayContain` member function of syntax element types (classes derived

Syntax elements may also have attributes. They should be initialized to `null` in the class definition (the TypeScript compiler will automatically generate a constructor to initialize them). The parser will construct an instance of each class it finds, and examine its automatically constructed dynamic members. During parsing, they will then be automatically initialized to attributes found in the schema being parsed. Unknown attributes will be ignored.

The XSD parser proceeds in stages (the parser and all syntax element classes have correspondingly named methods):

- `init` which calls `define` to bind named elements, attributes, types etc. to their scope and handles `import` and `include` declarations. The imports form a directed, possibly cyclic graph and can modify root scopes of arbitrary namespaces, so it's impossible to generally resolve references to other named things visible in scope before all imports have been processed.
- `resolve` which resolves references by name. Because possible attributes and child elements can be defined through deeply nested references that can point to other namespaces, it's generally impossible to know them all before all references in all namespaces have been resolved.
- TODO: `transform` which renames things to avoid naming conflicts between child elements and attributes (which will be merged into members of a single JSON object) and possibly deals with scope issues for TypeScript definition output.

TODO: after parsing, the resulting data structure should be exportable as JSON or a TypeScript definition file with ambient declarations of the XML namespaces.

License
=======

Expand Down
4 changes: 2 additions & 2 deletions src/xsd/Loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class Loader {
source.updateUrl(cached.url);
namespace.updateUrl(urlRemote, cached.url);

return(this.parser.preprocess(cached, source, this));
return(this.parser.init(cached, source, this));
}).then((dependencyList: Source[]) => {
// TODO: The source could be parsed already if all dependencies
// (and recursively their dependencies) have been preprocessed.
Expand All @@ -55,7 +55,7 @@ export class Loader {
}

finish() {
this.parser.finish();
this.parser.resolve();
this.resolve(this.targetNamespace);
}

Expand Down
8 changes: 4 additions & 4 deletions src/xsd/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class Parser {
return(state);
}

preprocess(cached: CacheResult, source: Source, loader: Loader) {
init(cached: CacheResult, source: Source, loader: Loader) {
var state = new State(null, this.rootRule, source);
var importList: {namespace: Namespace, url: string}[] = [];

Expand Down Expand Up @@ -132,7 +132,7 @@ export class Parser {
});

xml.on('endElement', function(name: string) {
if(state.xsdElement && state.xsdElement.finish) {
if(state.xsdElement && state.xsdElement.resolve) {
// Schedule finish hook to run after parsing is done.
// It might depend on definitions in scope but appearing later,
// and selectively postponing only hooks that cannot run yet
Expand Down Expand Up @@ -171,9 +171,9 @@ export class Parser {
return(promise);
}

finish() {
resolve() {
try {
this.pendingList.forEach((state: State) => state.xsdElement.finish(state));
this.pendingList.forEach((state: State) => state.xsdElement.resolve(state));
this.pendingList = [];
} catch(err) {
console.error(err);
Expand Down
10 changes: 5 additions & 5 deletions src/xsd/types/Attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export type XmlAttribute = string | number;

export class Attribute extends types.Base {
init(state: State) {
this.bind(state, 'attribute');
this.define(state, 'attribute');
this.surrogateKey = Attribute.nextKey++;
}

finish(state: State) {
resolve(state: State) {
var attribute = this;

if(this.ref) {
Expand All @@ -24,7 +24,7 @@ export class Attribute extends types.Base {
var ref = new QName(this.ref, state.source);
attribute = this.scope.lookup(ref, 'attribute');

if(attribute) attribute.bind(state, 'attribute', this.scope);
if(attribute) attribute.define(state, 'attribute', this.scope);
else throw new types.MissingReferenceError(this, state, 'attribute', ref);
}
}
Expand All @@ -48,10 +48,10 @@ export class AttributeGroup extends types.Base {
];

init(state: State) {
this.bind(state, 'attributegroup');
this.define(state, 'attributegroup');
}

finish(state: State) {
resolve(state: State) {
var attributeGroup = this;

if(this.ref) {
Expand Down
4 changes: 2 additions & 2 deletions src/xsd/types/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export class Base {
}

init(state: State) {}
finish(state: State) {}
resolve(state: State) {}

bind(state: State, type: string, scope?: Scope) {
define(state: State, type: string, scope?: Scope) {
if(this.name) (scope || this.scope).addToParent(new QName(this.name, state.source), type, this);
}

Expand Down
6 changes: 3 additions & 3 deletions src/xsd/types/ComplexType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as types from '../types';

export class TypeBase extends types.Base {
init(state: State) {
this.bind(state, 'type');
this.define(state, 'type');
this.scope.setType(this);
this.surrogateKey = TypeBase.nextKey++;
}
Expand Down Expand Up @@ -47,7 +47,7 @@ class ContentBase extends types.Base {
Restriction
]

finish(state: State) {
resolve(state: State) {
(state.parent.xsdElement as TypeBase).parent = this.parent;
}

Expand All @@ -68,7 +68,7 @@ export class ComplexContent extends ContentBase {
// Derived type support

export class XsdDerivationBase extends types.Base {
finish(state: State) {
resolve(state: State) {
var base = new QName(this.base, state.source);
(state.parent.xsdElement as ContentBase).parent = this.scope.lookup(base, 'type') as TypeBase || base;
}
Expand Down
6 changes: 3 additions & 3 deletions src/xsd/types/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ export class Element extends ElementBase {
];

init(state: State) {
this.bind(state, 'element');
this.define(state, 'element');
this.surrogateKey = Element.nextKey++;
}

finish(state: State) {
resolve(state: State) {
var element = this;

if(this.ref) {
Expand All @@ -33,7 +33,7 @@ export class Element extends ElementBase {
var ref = new QName(this.ref, state.source);
element = this.scope.lookup(ref, 'element');

if(element) element.bind(state, 'element', this.scope);
if(element) element.define(state, 'element', this.scope);
else throw new types.MissingReferenceError(this, state, 'element', ref);
}

Expand Down
6 changes: 3 additions & 3 deletions src/xsd/types/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class GenericChildList extends GroupBase {
Choice
];

finish(state: State) {
resolve(state: State) {
this.scope.addAllToParent('element');
}
}
Expand All @@ -40,10 +40,10 @@ export class Group extends GroupBase {
];

init(state: State) {
this.bind(state, 'group');
this.define(state, 'group');
}

finish(state: State) {
resolve(state: State) {
var group = this;

if(this.ref) {
Expand Down

0 comments on commit fd3c6a5

Please sign in to comment.