001package org.anarres.cpp;
002
003/*
004 * Anarres C Preprocessor
005 * Copyright (c) 2007-2015, Shevek
006 *
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
016 * or implied. See the License for the specific language governing
017 * permissions and limitations under the License.
018 */
019import javax.annotation.Nonnegative;
020import javax.annotation.Nonnull;
021import org.slf4j.Logger;
022import org.slf4j.LoggerFactory;
023
024/**
025 * A handler for preprocessor events, primarily errors and warnings.
026 *
027 * If no PreprocessorListener is installed in a Preprocessor, all
028 * error and warning events will throw an exception. Installing a
029 * listener allows more intelligent handling of these events.
030 */
031public class DefaultPreprocessorListener implements PreprocessorListener {
032
033    private static final Logger LOG = LoggerFactory.getLogger(DefaultPreprocessorListener.class);
034
035    private int errors;
036    private int warnings;
037
038    public DefaultPreprocessorListener() {
039        clear();
040    }
041
042    public void clear() {
043        errors = 0;
044        warnings = 0;
045    }
046
047    @Nonnegative
048    public int getErrors() {
049        return errors;
050    }
051
052    @Nonnegative
053    public int getWarnings() {
054        return warnings;
055    }
056
057    protected void print(@Nonnull String msg) {
058        LOG.info(msg);
059    }
060
061    /**
062     * Handles a warning.
063     *
064     * The behaviour of this method is defined by the
065     * implementation. It may simply record the error message, or
066     * it may throw an exception.
067     */
068    @Override
069    public void handleWarning(Source source, int line, int column,
070            String msg)
071            throws LexerException {
072        warnings++;
073        print(source.getName() + ":" + line + ":" + column
074                + ": warning: " + msg);
075    }
076
077    /**
078     * Handles an error.
079     *
080     * The behaviour of this method is defined by the
081     * implementation. It may simply record the error message, or
082     * it may throw an exception.
083     */
084    @Override
085    public void handleError(Source source, int line, int column,
086            String msg)
087            throws LexerException {
088        errors++;
089        print(source.getName() + ":" + line + ":" + column
090                + ": error: " + msg);
091    }
092
093    @Override
094    public void handleSourceChange(Source source, SourceChangeEvent event) {
095    }
096
097}