// helper method to wire Entityt -> Property -> ParseAction private ParseAction Add(Entity entity, Property prop, ParseAction consume) { entity.Add(prop); consume.Property = prop; prop.Source = consume; return(consume); }
// just recurse and provide a ParseAction for the nested Expression private ParseAction ImportGroupExpression(Expression exp, Entity entity, ParseAction parent = null, bool optional = false) { return(this.ImportPropertiesAndParseActions( ((GroupExpression)exp).Expression, entity, parent, optional )); }
private ParseAction ImportRepetitionExpression(Expression exp, Entity entity, ParseAction parent = null, bool optional = false) { RepetitionExpression repetition = ((RepetitionExpression)exp); // recurse down ParseAction action = this.ImportPropertiesAndParseActions( repetition.Expression, entity, parent, optional ); // mark Plural action.IsPlural = true; return(action); }
private ParseAction ImportOptionalExpression(Expression exp, Entity entity, ParseAction parent = null, bool optional = false) { OptionalExpression option = ((OptionalExpression)exp); // recurse down ParseAction consume = this.ImportPropertiesAndParseActions( option.Expression, entity, parent, true ); // mark optional consume.IsOptional = true; return(consume); }
private ParseAction ImportExtractorExpression(Expression exp, Entity entity, ParseAction parent = null, bool optional = false) { ExtractorExpression extr = ((ExtractorExpression)exp); return(this.Add( entity, new Property() { Name = extr.Name != null ? extr.Name : entity.Name }, new ConsumePattern() { Pattern = extr.Pattern, Parent = parent } )); }
private ParseAction ImportAlternativesExpression(Expression exp, Entity entity, ParseAction parent = null, bool optional = false) { AlternativesExpression alternative = ((AlternativesExpression)exp); ConsumeAny consume = new ConsumeAny() { Parent = parent }; // AlternativesExpression is constructed recusively, unroll it... while (true) { // add first part consume.Actions.Add(this.ImportPropertiesAndParseActions( alternative.NonAlternativesExpression, entity, consume, optional )); // add remaining parts if (alternative.Expression is NonAlternativesExpression) { // last part consume.Actions.Add(this.ImportPropertiesAndParseActions( alternative.Expression, entity, consume, optional )); break; } else { // recurse alternative = (AlternativesExpression)alternative.Expression; } } return(consume); }
private ParseAction ImportSequentialExpression(Expression exp, Entity entity, ParseAction parent = null, bool optional = false) { SequentialExpression sequence = ((SequentialExpression)exp); ConsumeAll consume = new ConsumeAll() { Parent = parent }; // SequentialExpression is constructed recusively, unroll it... while (true) { // add first part consume.Actions.Add(this.ImportPropertiesAndParseActions( sequence.AtomicExpression, entity, consume, optional )); // add remaining parts if (sequence.NonAlternativesExpression is AtomicExpression) { // last part consume.Actions.Add(this.ImportPropertiesAndParseActions( sequence.NonAlternativesExpression, entity, consume, optional )); break; } else { // recurse sequence = (SequentialExpression)sequence.NonAlternativesExpression; } } return(consume); }
private ParseAction ImportIdentifierExpression(Expression exp, Entity entity, ParseAction parent = null, bool optional = false) { IdentifierExpression id = ((IdentifierExpression)exp); if (!this.Model.Contains(id.Identifier)) { throw new ModelException("Unknown Entity reference: " + id.Identifier); } return(this.Add( entity, new Property() { Name = id.Name != null ? id.Name : id.Identifier }, new ConsumeEntity() { Reference = id.Identifier, Parent = parent } )); }
private ParseAction ImportPropertiesAndParseActions(Expression exp, Entity entity, ParseAction parent = null, bool optional = false) { this.Log("ImportPropertiesAndParseActions(" + exp.GetType().ToString() + ")"); try { return(new Dictionary <string, Func <Expression, Entity, ParseAction, bool, ParseAction> >() { { "AlternativesExpression", this.ImportAlternativesExpression }, { "SequentialExpression", this.ImportSequentialExpression }, { "OptionalExpression", this.ImportOptionalExpression }, { "RepetitionExpression", this.ImportRepetitionExpression }, { "GroupExpression", this.ImportGroupExpression }, { "IdentifierExpression", this.ImportIdentifierExpression }, { "StringExpression", this.ImportStringExpression }, { "ExtractorExpression", this.ImportExtractorExpression } }[exp.GetType().ToString()](exp, entity, parent, optional)); } catch (KeyNotFoundException e) { throw new NotImplementedException( "Importing not implemented for " + exp.GetType().ToString(), e ); } }
private ParseAction ImportStringExpression(Expression exp, Entity entity, ParseAction parent = null, bool optional = false) { StringExpression str = ((StringExpression)exp); ParseAction consume = new ConsumeString() { String = str.String, Parent = parent }; // default behavior for String Consumption is to just do it, it must be // done to have succesful parsing // IF the string being parsed is within an optional context, we migt want // to know IF it was parsed. So we create a property to store a flag to // indicate whether the optional parse was successful or not. // IF the StringExpression has an explicit Name (name @ string-expression) // we _do_ create a property with that name. // IF the StringExpression has an Alternatives Parent we behave as if we // are optional. if (parent is ConsumeAny) { optional = true; } // UNLESS the name is an underscore '_', then we don't create any Property if (str.Name != null && str.Name.Equals("_")) { return(consume); } if (str.Name != null || optional) { if (optional) { consume.ReportSuccess = true; } string name = (str.Name != null ? str.Name : consume.Name); // TODO improve and extract if (name.Equals("-")) { name = "dash"; } name = name.Replace("(", "open-bracket"); name = name.Replace(")", "close-bracket"); name = name.Replace("[", "open-square-bracket"); name = name.Replace("]", "close-square-bracket"); name = name.Replace("{", "open-brace"); name = name.Replace("}", "close-brace"); name = name.Replace(":", "colon"); name = name.Replace("=", "equals"); name = name.Replace("&", "ampersand"); name = name.Replace("<", "less-than"); name = name.Replace(">", "greater-than"); name = name.Replace("+", "plus"); name = name.Replace("*", "star"); name = name.Replace("!", "exclamation"); name = name.Replace(";", "semi-colon"); name = name.Replace(",", "comma"); name = name.Replace(".", "dot"); return(this.Add( entity, new Property() { Name = (optional ? "has-" : "") + name }, consume )); } // a simple consumer of text, with no resulting information return(consume); }