Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9127639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:19:40+00:00 2026-06-17T07:19:40+00:00

I try to write the Xtext BNF for Configuration files (known with the .ini

  • 0

I try to write the Xtext BNF for Configuration files (known with the .ini extension)

For instance, I’d like to successfully parse

[Section1]
a = Easy123
b = This *is* valid too

[Section_2]
c = Voilà # inline comments are ignored

My problem is matching the property value (what’s on the right of the ‘=’).

My current grammar works if the property matches the ID terminal (eg a = Easy123).

PropertyFile hidden(SL_COMMENT, WS):
    sections+=Section*;

Section:
    '[' name=ID ']'
    (NEWLINE properties+=Property)+
    NEWLINE+;

Property:
    name=ID (':' | '=') value=ID ';'?;

terminal WS:
    (' ' | '\t')+;

terminal NEWLINE:
// New line on DOS or Unix 
    '\r'? '\n';

terminal ID:
    ('A'..'Z' | 'a'..'z') ('A'..'Z' | 'a'..'z' | '_' | '-' | '0'..'9')*;

terminal SL_COMMENT:
// Single line comment
    '#' !('\n' | '\r')*;

I don’t know how to generalize the grammar to match any text (eg c = Voilà).

I certainly need to introduce a new terminal
Property:
name=ID (‘:’ | ‘=’) value=TEXT ‘;’?;

Question is: how should I define this TEXT terminal?

I have tried

  • terminal TEXT: ANY_OTHER+;
    This raises a warning

    The following token definitions can never be matched because prior tokens match the same input: RULE_INT,RULE_STRING,RULE_ML_COMMENT,RULE_ANY_OTHER

    (I think it doesn’t matter).

    Parsing Fails with

    Required loop (…)+ did not match anything at input ‘à’

  • terminal TEXT: !('\r'|'\n'|'#')+;
    This raises a warning

    The following token definitions can never be matched because prior tokens match the same input: RULE_INT

    (I think it doesn’t matter).

    Parsing Fails with

    Missing EOF at [Section1]

  • terminal TEXT: ('!'|'$'..'~'); (which covers most characters, except # and ")
    No warning during the generation of the lexer/parser.
    However Parsing Fails with

    Mismatch input ‘Easy123’ expecting RULE_TEXT

    Extraneous input ‘This’ expecting RULE_TEXT

    Required loop (…)+ did not match anything at ‘is’

Thanks for your help (and I hope this grammar can be useful for others too)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:19:42+00:00Added an answer on June 17, 2026 at 7:19 am

    This grammar does the trick:

    grammar org.xtext.example.mydsl.MyDsl hidden(SL_COMMENT, WS)
    
    generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
    import "http://www.eclipse.org/emf/2002/Ecore"
    
    PropertyFile:
        sections+=Section*;
    
    Section:
        '[' name=ID ']' 
        (NEWLINE+ properties+=Property)+
        NEWLINE+;
    
    Property:
        name=ID value=PROPERTY_VALUE;
    
    terminal PROPERTY_VALUE: (':' | '=') !('\n' | '\r')*;
    
    terminal WS:
        (' ' | '\t')+;
    
    terminal NEWLINE:
    // New line on DOS or Unix 
        '\r'? '\n';
    
    terminal ID:
        ('A'..'Z' | 'a'..'z') ('A'..'Z' | 'a'..'z' | '_' | '-' | '0'..'9')*;
    
    terminal SL_COMMENT:
    // Single line comment
        '#' !('\n' | '\r')*;
    

    Key is, that you do not try to cover the complete semantics only in the grammar but take other services into account, too. The terminal rule PROPERTY_VALUE consumes the complete value including leading assignment and optional trailing semicolon.

    Now just register a value converter service for that language and take care of the insignificant parts of the input, there:

    import org.eclipse.xtext.conversion.IValueConverter;
    import org.eclipse.xtext.conversion.ValueConverter;
    import org.eclipse.xtext.conversion.ValueConverterException;
    import org.eclipse.xtext.conversion.impl.AbstractDeclarativeValueConverterService;
    import org.eclipse.xtext.conversion.impl.AbstractIDValueConverter;
    import org.eclipse.xtext.conversion.impl.AbstractLexerBasedConverter;
    import org.eclipse.xtext.nodemodel.INode;
    import org.eclipse.xtext.util.Strings;
    
    import com.google.inject.Inject;
    
    public class PropertyConverters extends AbstractDeclarativeValueConverterService {
        @Inject
        private AbstractIDValueConverter idValueConverter;
    
        @ValueConverter(rule = "ID")
        public IValueConverter<String> ID() {
            return idValueConverter;
        }
    
        @Inject
        private PropertyValueConverter propertyValueConverter;
    
        @ValueConverter(rule = "PROPERTY_VALUE")
        public IValueConverter<String> PropertyValue() {
            return propertyValueConverter;
        }
    
        public static class PropertyValueConverter extends AbstractLexerBasedConverter<String> {
    
            @Override
            protected String toEscapedString(String value) {
                return " = " + Strings.convertToJavaString(value, false);
            }
    
            public String toValue(String string, INode node) {
                if (string == null)
                    return null;
                try {
                    String value = string.substring(1).trim();
                    if (value.endsWith(";")) {
                        value = value.substring(0, value.length() - 1);
                    }
                    return value;
                } catch (IllegalArgumentException e) {
                    throw new ValueConverterException(e.getMessage(), node, e);
                }
            }
        }
    }
    

    The follow test case will succeed, after you registered the service in the runtime module like this:

    @Override
    public Class<? extends IValueConverterService> bindIValueConverterService() {
        return PropertyConverters.class;
    }
    

    Test case:

    import org.junit.runner.RunWith
    import org.eclipse.xtext.junit4.XtextRunner
    import org.xtext.example.mydsl.MyDslInjectorProvider
    import org.eclipse.xtext.junit4.InjectWith
    import org.junit.Test
    import org.eclipse.xtext.junit4.util.ParseHelper
    import com.google.inject.Inject
    import org.xtext.example.mydsl.myDsl.PropertyFile
    import static org.junit.Assert.*
    
    @RunWith(typeof(XtextRunner))
    @InjectWith(typeof(MyDslInjectorProvider))
    class ParserTest {
    
        @Inject
        ParseHelper<PropertyFile> helper
    
        @Test
        def void testSample() {
            val file = helper.parse('''
                [Section1]
                a = Easy123
                b : This *is* valid too;
    
                [Section_2]
                # comment
                c = Voilà # inline comments are ignored
            ''')
            assertEquals(2, file.sections.size)
            val section1 = file.sections.head
            assertEquals(2, section1.properties.size)
            assertEquals("a", section1.properties.head.name)
            assertEquals("Easy123", section1.properties.head.value)
            assertEquals("b", section1.properties.last.name)
            assertEquals("This *is* valid too", section1.properties.last.value)
    
            val section2 = file.sections.last
            assertEquals(1, section2.properties.size)
            assertEquals("Voilà # inline comments are ignored", section2.properties.head.value)
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to write DSL class Warcraft class << self def config unless @instance
I want to try write a simple kernel in C# like cosmos, just for
i try to write my own control.But my control' html has problem: Look like
I try to write to a large file, but it seems like it does
try to write a composite component that allows mutltiple text inputs. I read that
I try to write Activator action for changing current song rating. I now that
I try to write a Facebook app. This app will communicate with a rfid
I try to write a javascript Self-Executing Anonymous Function window.App = window.App || {}
I try to write a macro in clojure that sets up a namespace and
I try to write a simple user script to enlarge the picture when you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.