/* (c) https://github.com/MontiCore/monticore */ /** * This grammar defines Java compliant literals. The scope of this grammar is to * ease the reuse of literals structures in Java-like sublanguages, e.g., by * grammar inheritance or grammar embedment. * The grammar contains literals from Java for: Char and String */ component grammar StringLiterals extends de.monticore.MCBasics { /*========================================================================*/ /*== Characters ==========================================================*/ /*========================================================================*/ /** ASTCharLiteral represents any valid character parenthesized with "'". @attribute source String-representation (excluding "'"). */ CharLiteral = source:CharToken; astrule CharLiteral = method public char getValue() { return de.monticore.literals.MCLiteralsDecoder.decodeChar(getSource()); } ; token CharToken = '\'' (SingleCharacter|EscapeSequence) '\'' : {setText(getText().substring(1, getText().length() - 1));}; /*========================================================================*/ /*== String ==============================================================*/ /*========================================================================*/ /** ASTStringLiteral represents any valid character sequence parenthesized with '"'. @attribute source String-representation (excluding '"'). @attribute content decoded String-representation (no '"' and escapes are decoded). */ StringLiteral = source:StringToken; astrule StringLiteral = // Caution: decoded value is in cache: so change of value leads // to outdated result in getValue() content:String method public String getValue() { if(content == null) { content = de.monticore.literals.MCLiteralsDecoder.decodeString(getSource()); } return content; }; token StringToken = '"' (StringCharacters)? '"' : {setText(getText().substring(1, getText().length() - 1));}; /*========================================================================*/ /*============================ LEXER RULES ===============================*/ /*========================================================================*/ fragment token HexDigit = '0'..'9' | 'a'..'f' | 'A'..'F' ; fragment token OctalDigit = '0'..'7' ; fragment token SingleCharacter = ~ ('\''); fragment token StringCharacters = (StringCharacter)+; fragment token StringCharacter = ~ ('"' | '\\') | EscapeSequence; // ยง3.10.6 Escape Sequences for Character and String Literals fragment token EscapeSequence = '\\' ('b' | 't' | 'n' | 'f' | 'r' | '"' | '\'' | '\\') | OctalEscape | UnicodeEscape; fragment token OctalEscape = '\\' OctalDigit | '\\' OctalDigit OctalDigit | '\\' ZeroToThree OctalDigit OctalDigit; fragment token UnicodeEscape = '\\' 'u' HexDigit HexDigit HexDigit HexDigit; fragment token ZeroToThree = '0'..'3' ; }