Search in sources :

Example 1 with RegExp

use of org.whole.lang.grammars.model.RegExp in project whole by wholeplatform.

the class GrammarsValidatorVisitor method checkPatterns.

public void checkPatterns(Grammar entity) {
    AbstractPatternFilterIterator<RegExp> i = IteratorFactory.<RegExp>descendantOrSelfMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.RegExp);
    i.reset(entity);
    for (RegExp regex : i) {
        try {
            Pattern.compile(regex.getValue());
        } catch (PatternSyntaxException e) {
            getDecorationManager().addError(regex, e.toString(), regex.getValue());
        }
    }
}
Also used : RegExp(org.whole.lang.grammars.model.RegExp) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 2 with RegExp

use of org.whole.lang.grammars.model.RegExp in project whole by wholeplatform.

the class GrammarsDefaultDataTypeParser method matches.

protected String matches(EntityDescriptor<?> ed, String value) {
    Pattern pattern = grammarBasedDataTypeParser.getDataTerminal(ed).getPattern();
    java.util.regex.Pattern compiledPattern = Matcher.matchImpl(GrammarsEntityDescriptorEnum.CompiledPattern, pattern) ? ((CompiledPattern) pattern).getValue() : java.util.regex.Pattern.compile(((RegExp) pattern).getValue());
    if (!compiledPattern.matcher(value).matches())
        throw new WholeIllegalArgumentException(WholeMessages.no_data_type);
    return value;
}
Also used : CompiledPattern(org.whole.lang.grammars.model.CompiledPattern) Pattern(org.whole.lang.grammars.model.Pattern) RegExp(org.whole.lang.grammars.model.RegExp) WholeIllegalArgumentException(org.whole.lang.exceptions.WholeIllegalArgumentException)

Example 3 with RegExp

use of org.whole.lang.grammars.model.RegExp in project whole by wholeplatform.

the class GrammarsDefaultEntityRegistryConfiguration method apply.

public void apply(IEntityRegistry er) {
    super.apply(er);
    GrammarsEntityFactory ef = GrammarsEntityFactory.instance(er);
    RegExp regex;
    Literal literal;
    IdentityEventHandler deriveLiteralPattern = new IdentityEventHandler() {

        private static final long serialVersionUID = 1L;

        private String derivedPattern;

        @Override
        public void notifyChanged(IEntity source, FeatureDescriptor featureDesc, String oldValue, String newValue) {
            IEntity parent = source.wGetParent();
            if (parent instanceof LiteralTerminal) {
                LiteralTerminal literalTerminal = (LiteralTerminal) parent;
                Literal literal = literalTerminal.getLiteral();
                if (literal == source) {
                    derivedPattern = StringUtils.quoteRegex(newValue);
                    literalTerminal.getPattern().wSetValue(derivedPattern);
                } else if (newValue != derivedPattern) {
                    source.wRemoveChangeEventHandler(this);
                    literal.wRemoveChangeEventHandler(this);
                }
            }
        }
    };
    er.put(ef.createEmpty(ef.createLiteral("")));
    er.put(ef.createChoose(2));
    er.put(ef.createConcatenate(2));
    er.put(ef.createLiteralTerminal(regex = ef.createRegExp("pattern"), ef.createCategory(CategoryEnum.DELIMITER), literal = ef.createLiteral("literal")));
    regex.wAddChangeEventHandler(deriveLiteralPattern);
    literal.wAddChangeEventHandler(deriveLiteralPattern);
    er.put(ef.createDataTerminal(ef.createRegExp(".*"), ef.createCategory(CategoryEnum.LITERAL), ef.createFormat("%s")));
    er.put(ef.createAs(ef.createNonTerminal("nt"), ef.createName("name")));
    er.put(ef.createByLines(1));
    er.put(ef.createBySize(10));
    er.put(ef.createRepeat(ef.createSize(0), ef.createUnbounded(), ef.buildLiteralTerminal().set(GrammarsFeatureDescriptorEnum.literal, ",").getResult(), createResolver(GrammarsEntityDescriptorEnum.Rule)));
    er.put(ef.createBySize(5));
    er.put(ef.createGrammar(ef.createName("GrammarName"), createResolver(GrammarsEntityDescriptorEnum.TargetLanguage), createResolver(GrammarsEntityDescriptorEnum.NonTerminal), ef.createLiteralTerminal(ef.createRegExp("\\p{javaWhitespace}*"), ef.createCategory(CategoryEnum.SEPARATOR), ef.createLiteral("")), ef.createProductions(0), ef.createProductions(ef.create(GrammarsEntityDescriptorEnum.Production, ef.createNonTerminal("StartSymbol"), createResolver(GrammarsEntityDescriptorEnum.Rule))), // ef.createNamespace("org.whole.examples.grammarname"),
    createResolver(GrammarsEntityDescriptorEnum.URI), createResolver(GrammarsEntityDescriptorEnum.Namespace), createResolver(GrammarsEntityDescriptorEnum.Version), createResolver(GrammarsEntityDescriptorEnum.Literal), createResolver(GrammarsEntityDescriptorEnum.Literal), createResolver(GrammarsEntityDescriptorEnum.Literal)));
}
Also used : IdentityEventHandler(org.whole.lang.events.IdentityEventHandler) RegExp(org.whole.lang.grammars.model.RegExp) IEntity(org.whole.lang.model.IEntity) FeatureDescriptor(org.whole.lang.reflect.FeatureDescriptor) Literal(org.whole.lang.grammars.model.Literal) LiteralTerminal(org.whole.lang.grammars.model.LiteralTerminal)

Aggregations

RegExp (org.whole.lang.grammars.model.RegExp)3 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 IdentityEventHandler (org.whole.lang.events.IdentityEventHandler)1 WholeIllegalArgumentException (org.whole.lang.exceptions.WholeIllegalArgumentException)1 CompiledPattern (org.whole.lang.grammars.model.CompiledPattern)1 Literal (org.whole.lang.grammars.model.Literal)1 LiteralTerminal (org.whole.lang.grammars.model.LiteralTerminal)1 Pattern (org.whole.lang.grammars.model.Pattern)1 IEntity (org.whole.lang.model.IEntity)1 FeatureDescriptor (org.whole.lang.reflect.FeatureDescriptor)1