/* (c) https://github.com/MontiCore/monticore */ package de.monticore.literals; /* This is a MontiCore stable grammar. * Adaptations -- if any -- are conservative. */ import de.monticore.literals.*; /** * * 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, e.g., Boolean, Char, String, .... * Please note that Java has an extended syntax e.g. for integers * using underscores or other kinds of encodings. * They parse e.g. 999_999 or 0x3F2A * */ component grammar MCJavaLiterals extends MCCommonLiterals { /*========================================================================*/ /*======================= INTERFACE DEFINITIONS ==========================*/ /*========================================================================*/ /*========================================================================*/ /*============================ PARSER RULES ==============================*/ /*========================================================================*/ /** ASTIntLiteral represents a positive Integer number. @attribute source String-representation (including '"'). */ IntLiteral implements NumericLiteral <100> = source:Num_Int ; /** ASTLongLiteral represents a positive Long number. @attribute source String-representation (including '"'). */ LongLiteral implements NumericLiteral <99> = source:Num_Long ; /** ASTFloatLiteral represents a positive Float number. @attribute source String-representation (including '"'). */ FloatLiteral implements NumericLiteral <100> = source:Num_Float ; /** ASTDoubleLiteral represents a positive Double number. @attribute source String-representation (including '"'). */ DoubleLiteral implements NumericLiteral <100> = source:Num_Double ; /*========================================================================*/ /*============================ LEXER RULES ===============================*/ /*========================================================================*/ /*========================================================================*/ /* The following section is adapted from */ /* https://github.com/antlr/grammars-v4/blob/master/java/Java.g4 */ /*========================================================================*/ // §3.10.1 Integer Literals token Num_Int = DecimalIntegerLiteral | HexIntegerLiteral | OctalIntegerLiteral | BinaryIntegerLiteral; token Num_Long = DecimalIntegerLiteral IntegerTypeSuffix | HexIntegerLiteral IntegerTypeSuffix | OctalIntegerLiteral IntegerTypeSuffix | BinaryIntegerLiteral IntegerTypeSuffix; fragment token DecimalIntegerLiteral = DecimalNumeral; fragment token HexIntegerLiteral = HexNumeral; fragment token OctalIntegerLiteral = OctalNumeral; fragment token BinaryIntegerLiteral = BinaryNumeral; fragment token IntegerTypeSuffix = 'l' | 'L'; fragment token DecimalNumeral = '0' | NonZeroDigit (Digits? | Underscores Digits); @Override fragment token Digits = Digit (DigitOrUnderscore* Digit)?; @Override fragment token Digit = '0' | NonZeroDigit; fragment token NonZeroDigit = '1'..'9' ; fragment token DigitOrUnderscore = Digit | '_'; fragment token Underscores = '_'+; fragment token HexNumeral = '0' ('x' | 'X') HexDigits; fragment token HexDigits = HexDigit (HexDigitOrUnderscore* HexDigit)?; @Override fragment token HexDigit = '0'..'9' | 'a'..'f' | 'A'..'F' ; fragment token HexDigitOrUnderscore = HexDigit | '_'; fragment token OctalNumeral = '0' Underscores? OctalDigits; fragment token OctalDigits = OctalDigit (OctalDigitOrUnderscore* OctalDigit)?; @Override fragment token OctalDigit = '0'..'7' ; fragment token OctalDigitOrUnderscore = OctalDigit | '_'; fragment token BinaryNumeral = '0' ('b' | 'B') BinaryDigits; fragment token BinaryDigits = BinaryDigit (BinaryDigitOrUnderscore* BinaryDigit)?; fragment token BinaryDigit = '0' | '1'; fragment token BinaryDigitOrUnderscore = BinaryDigit | '_'; // §3.10.2 Floating-Point Literals token Num_Float = DecimalFloatingPointLiteral | HexadecimalFloatingPointLiteral; token Num_Double = DecimalDoublePointLiteral | HexadecimalDoublePointLiteral; fragment token DecimalDoublePointLiteral = Digits '.' Digits? ExponentPart? DoubleTypeSuffix? | '.' Digits ExponentPart? DoubleTypeSuffix? | Digits ExponentPart DoubleTypeSuffix? | Digits DoubleTypeSuffix; fragment token DecimalFloatingPointLiteral = Digits '.' Digits? ExponentPart? FloatTypeSuffix | '.' Digits ExponentPart? FloatTypeSuffix | Digits ExponentPart FloatTypeSuffix | Digits FloatTypeSuffix; fragment token ExponentPart = ExponentIndicator SignedInteger; fragment token ExponentIndicator = 'e' | 'E'; fragment token SignedInteger = Sign? Digits; fragment token Sign = '+' | '-'; fragment token FloatTypeSuffix = 'f' | 'F'; fragment token DoubleTypeSuffix = 'd' | 'D'; fragment token HexadecimalDoublePointLiteral = HexSignificand BinaryExponent DoubleTypeSuffix?; fragment token HexadecimalFloatingPointLiteral = HexSignificand BinaryExponent FloatTypeSuffix; fragment token HexSignificand = HexNumeral '.'? | '0' ('x' | 'X') HexDigits? '.' HexDigits; fragment token BinaryExponent = BinaryExponentIndicator SignedInteger; fragment token BinaryExponentIndicator = 'p' | 'P'; /*========================================================================*/ /*======================= AST DEFINITIONS ================================*/ /*========================================================================*/ astrule IntLiteral = method public int getValue() { return de.monticore.literals.MCLiteralsDecoder.decodeInt(getSource()); } ; astrule LongLiteral = method public long getValue() { return de.monticore.literals.MCLiteralsDecoder.decodeLong(getSource()); } ; astrule FloatLiteral = method public float getValue() { return de.monticore.literals.MCLiteralsDecoder.decodeFloat(getSource()); } ; astrule DoubleLiteral = method public double getValue() { return de.monticore.literals.MCLiteralsDecoder.decodeDouble(getSource()); } ; }