private static CSSTreeParser createParser(Object source, String encoding, SourceType type, Preparator preparator, StyleSheet stylesheet, URL base) throws IOException, CSSException { CSSInputStream input = type.getInput(source, encoding); input.setBase(base); CommonTokenStream tokens = feedLexer(input, stylesheet); CommonTree ast = feedParser(tokens, type, stylesheet); return feedAST(tokens, ast, preparator, stylesheet); }
public CSSToken make() { CSSToken t = new CSSToken(input, state.type, state.channel, state.tokenStartCharIndex, input.index()-1, lexerClass); t.setLine(state.tokenStartLine); t.setText(state.text); t.setCharPositionInLine(state.tokenStartCharPositionInLine); t.setBase(input.getBase()); // clone lexer state t.setLexerState(new CSSLexerState(ls)); return t; } }
private static CSSTreeParser createTreeParser(Object source, NetworkProcessor network, String encoding, SourceType type, Preparator preparator, URL base, List<MediaQuery> media) throws IOException, CSSException { CSSInputStream input = getInput(source, network, encoding, type); input.setBase(base); CommonTokenStream tokens = feedLexer(input); CommonTree ast = feedParser(tokens, type); return feedAST(tokens, ast, preparator, media); }
/** * Recovers and logs error, prepares tree part replacement */ private Object invalidFallback(int ttype, String ttext, RecognitionException re) { reportError(re); recover(input, re); return invalidReplacement(ttype, ttext); }
@Override public CommonTree getAST(CSSParser parser) throws CSSException { try { CSSParser.stylesheet_return retval = parser.stylesheet(); return (CommonTree) retval.getTree(); } catch (RecognitionException re) { throw encapsulateException(re, "Unable to parse URL CSS style"); } catch (RuntimeException re) { throw encapsulateException(re, "Unable to parse URL CSS style"); } }
@Override public CommonTree getAST(CSSParser parser) throws CSSException { try { CSSParser.inlinestyle_return retval = parser.inlinestyle(); return (CommonTree) retval.getTree(); } catch (RecognitionException re) { throw encapsulateException(re, "Unable to parse inline CSS style"); } catch (RuntimeException re) { throw encapsulateException(re, "Unable to parse inline CSS style"); } }
/** * This function must be called to initialize lexer's state. * Because we can't change directly generated constructors. */ public void init() { this.log = org.slf4j.LoggerFactory.getLogger(getClass()); this.ls = new cz.vutbr.web.csskit.antlr.CSSLexerState(); this.tf = new cz.vutbr.web.csskit.antlr.CSSTokenFactory(input, state, ls, getClass()); this.tr = new cz.vutbr.web.csskit.antlr.CSSTokenRecovery(this, input, state, ls, log); this.er = new cz.vutbr.web.csskit.antlr.CSSExpressionReader(input, log); }
@Override public StyleSheet parse(CSSTreeParser parser) throws CSSException { try { return parser.stylesheet(); } catch (RecognitionException re) { throw encapsulateException(re, "Unable to parse file CSS style [AST]"); } catch (RuntimeException re) { throw encapsulateException(re, "Unable to parse file CSS style [AST]"); } }
@Override public StyleSheet parse(CSSTreeParser parser) throws CSSException { try { return parser.inlinestyle(); } catch (RecognitionException re) { throw encapsulateException(re, "Unable to parse inline CSS style [AST]"); } catch (RuntimeException re) { throw encapsulateException(re, "Unable to parse inline CSS style [AST]"); } }
/** * Obtains the current lexer state from current token */ private cz.vutbr.web.csskit.antlr.CSSLexerState getCurrentLexerState(Token t) { if (t instanceof cz.vutbr.web.csskit.antlr.CSSToken) return ((cz.vutbr.web.csskit.antlr.CSSToken) t).getLexerState(); else return null; }
/** * Creates CSSToken, this is base {@code emit()} constructor * @param input Input stream * @param type Type of token * @param channel Channel of token * @param start Start position in stream * @param stop End position in stream */ public CSSToken(CharStream input, int type, int channel, int start, int stop, Class<? extends Lexer> lexerClass) { super(input, type, channel, start, stop); typeMapper = new TypeMapper(CSSToken.class, lexerClass, "FUNCTION", "URI", "STRING", "CLASSKEYWORD", "HASH"); }
/** * Creates CSSToken of given type with cloning lexer state * automatically, allows to set text boundaries in input stream * @param type Type of token * @param state State of lexer, which will be copied * @param start Start position in stream * @param stop End position in stream */ public CSSToken(int type, CSSLexerState state, int start, int stop, Class<? extends Lexer> lexerClass) { this(null, type, Token.DEFAULT_CHANNEL, start, stop, lexerClass); this.ls = new CSSLexerState(state); }
/** * Returns CSSParserFactory registered in step above * * @return CSSParserFactory registered */ private static final CSSParserFactory getCSSParserFactory() { if (pf == null) pf = CSSParserFactory.getInstance(); return pf; }
@Override public void emitErrorMessage(String msg) { warn("ANTLR: {}", msg); }
public static CSSParserFactory getInstance() { if(instance == null) instance = new CSSParserFactory(); return instance; }
@Override public CommonTree getAST(CSSParser parser) throws CSSException { try { CSSParser.stylesheet_return retval = parser.stylesheet(); return (CommonTree) retval.getTree(); } catch (RecognitionException re) { throw encapsulateException(re, "Unable to parse embedded CSS style"); } catch (RuntimeException re) { throw encapsulateException(re, "Unable to parse embedded CSS style"); } }
@Override public StyleSheet parse(CSSTreeParser parser) throws CSSException { try { return parser.stylesheet(); } catch (RecognitionException re) { throw encapsulateException(re, "Unable to parse embedded CSS style [AST]"); } catch (RuntimeException re) { throw encapsulateException(re, "Unable to parse embedded CSS style [AST]"); } }