Skip to content

Error trying to highlight Java code with a "record" local variable #2016

@aaime

Description

@aaime

Running pygmentize against this file:

pygmentize -v -F raiseonerror   -l java /home/aaime/devel/git-gt/docs/src/main/java/org/geotools/tutorial/csv3/parse/CSVStrategy.java
pygmentize -V
Pygments version 2.11.1, (c) 2006-2021 by Georg Brandl, Matthäus Chajdas and contributors.
/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 * 	  (C) 2014 - 2015 Open Source Geospatial Foundation (OSGeo)
 * 	  (c) 2012 - 2014 OpenPlans
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */
package org.geotools.tutorial.csv3.parse;

import com.csvreader.CsvReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.tutorial.csv3.CSVFileState;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

// docs start CSVStrategy
public abstract class CSVStrategy {

    protected final CSVFileState csvFileState;

    public CSVStrategy(CSVFileState csvFileState) {
        this.csvFileState = csvFileState;
    }

    public CSVIterator iterator() throws IOException {
        return new CSVIterator(csvFileState, this);
    }
    // docs end CSVStrategy

    protected abstract SimpleFeatureType buildFeatureType();

    public abstract void createSchema(SimpleFeatureType featureType) throws IOException;

    public abstract SimpleFeature decode(String recordId, String[] csvRecord);

    public abstract String[] encode(SimpleFeature feature);

    protected volatile SimpleFeatureType featureType = null;

    public SimpleFeatureType getFeatureType() {
        if (featureType == null) {
            synchronized (this) {
                if (featureType == null) {
                    featureType = buildFeatureType();
                }
            }
        }
        return featureType;
    }

    // docs start createBuilder
    /**
     * Originally in a strategy support class - giving a chance to override them to improve
     * efficiency and utilize the different strategies
     */
    public static SimpleFeatureTypeBuilder createBuilder(CSVFileState csvFileState) {
        CsvReader csvReader = null;
        Map<String, Class<?>> typesFromData = null;
        String[] headers = null;
        try {
            csvReader = csvFileState.openCSVReader();
            headers = csvReader.getHeaders();
            typesFromData = findMostSpecificTypesFromData(csvReader, headers);
        } catch (IOException e) {
            throw new RuntimeException("Failure reading csv file", e);
        } finally {
            if (csvReader != null) {
                csvReader.close();
            }
        }
        return createBuilder(csvFileState, headers, typesFromData);
    }

    public static SimpleFeatureTypeBuilder createBuilder(
            CSVFileState csvFileState, String[] headers, Map<String, Class<?>> typesFromData) {
        SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
        builder.setName(csvFileState.getTypeName());
        builder.setCRS(csvFileState.getCrs());
        if (csvFileState.getNamespace() != null) {
            builder.setNamespaceURI(csvFileState.getNamespace());
        }
        for (String col : headers) {
            Class<?> type = typesFromData.get(col);
            builder.add(col, type);
        }
        return builder;
    }

    /**
     * Performs a full file scan attempting to guess the type of each column Specific strategy
     * implementations will expand this functionality by overriding the buildFeatureType() method.
     */
    protected static Map
*****************************************************************
An unhandled exception occurred while highlighting.
Please report the whole traceback to the issue tracker at
<https://github.com/pygments/pygments/issues>.
*****************************************************************

<String, Class<?>> findMostSpecificTypesFromData(
            CsvReader csvReader, String[] headers) throws IOException {
        Map<String, Class<?>> result = new HashMap<>();
        // start off assuming Integers for everything
        for (String header : headers) {
            result.put(header, Integer.class);
        }
        // Read through the whole file in case the type changes in later rows
        while (csvReader.readRecord()) {
            String[] record Traceback (most recent call last):
  File "/usr/local/bin/pygmentize", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.8/dist-packages/pygments/cmdline.py", line 638, in main
    return main_inner(parser, argns)
  File "/usr/local/lib/python3.8/dist-packages/pygments/cmdline.py", line 490, in main_inner
    highlight(code, lexer, fmter, outfile)
  File "/usr/local/lib/python3.8/dist-packages/pygments/__init__.py", line 82, in highlight
    return format(lex(code, lexer), formatter, outfile)
  File "/usr/local/lib/python3.8/dist-packages/pygments/__init__.py", line 64, in format
    formatter.format(tokens, outfile)
  File "/usr/local/lib/python3.8/dist-packages/pygments/formatters/terminal256.py", line 250, in format
    return Formatter.format(self, tokensource, outfile)
  File "/usr/local/lib/python3.8/dist-packages/pygments/formatter.py", line 94, in format
    return self.format_unencoded(tokensource, outfile)
  File "/usr/local/lib/python3.8/dist-packages/pygments/formatters/terminal256.py", line 256, in format_unencoded
    for ttype, value in tokensource:
  File "/usr/local/lib/python3.8/dist-packages/pygments/filter.py", line 19, in _apply
    yield from filter_.filter(lexer, stream)
  File "/usr/local/lib/python3.8/dist-packages/pygments/filters/__init__.py", line 782, in filter
    raise self.exception(value)
pygments.filters.ErrorToken: =

My guess is that the "record" local variable is treated as a keyword. However, the Java 17 compiler does not complain. My guess is that "record" did not become a fully reserved keyword, and the compiler has some backwards compatibility support for it.

By the way, the GeoTools project successfully builds with Java 8, 11 and 17.
We noticed the issue recently as part of our Sphinx build, which internally uses pygments to highlight code examples:
geotools/geotools#3724
The Github action picks the latest Sphinx, and its dependencies, for each and every build, so the regression must be very recent. The code failing to be highlighted has not seen recent changes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions