Changeset 35291 in osm for applications/editors


Ignore:
Timestamp:
2020-01-13T21:45:35+01:00 (5 years ago)
Author:
simon04
Message:

JOSM/comfort0: Level0LParser.jj <WHITESPACE>

Location:
applications/editors/josm/plugins/comfort0
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/comfort0/src/net/simon04/comfort0/level0l/Level0LParser.jj

    r35290 r35291  
    11options {
    22  STATIC = false;
    3   OUTPUT_DIRECTORY = "parsergen";
    43}
    54
     
    1514
    1615<DEFAULT> //HEADER
    17 SKIP: { " " }
    18 
    19 <DEFAULT> //HEADER
    2016TOKEN: {
    21     <H_EOL: ("\n" | "\r" | "\r\n") > : DATA
    22     | <H_ID: (["0"-"9"])+ >
     17    <EOL: ("\n" | "\r" | "\r\n") >
     18    | <ID: (["0"-"9"])+ >
    2319    | <NODE: "node" >
    2420    | <WAY: "way" >
    2521    | <RELATION: "relation" >
     22    | <WY: "wy" >
     23    | <ND: "nd" >
     24    | <REL: "rel" >
     25    | <EQ: "=" > : VALUE
     26    | <IDENT: ["a"-"z", "A"-"Z", "_"] (["a"-"z", "A"-"Z", "_", ":", "-", "0"-"9"])* >
    2627    | <FLOAT: ("+" | "-")? (["0"-"9"])+ ("." (["0"-"9"])+)? >
    2728    | <COLON: ":" >
    2829    | <COMMA: "," >
     30    | <WHITESPACE: ([" ", "\t"])+ >
    2931    | <COMMENT_START: "#"> : COMMENT
    3032}
     
    3537<COMMENT>
    3638TOKEN: {
    37     <C_EOL: ("\n" | "\r" | "\r\n") > : DATA
    38 }
    39 
    40 <DATA>
    41 TOKEN: {
    42     <D_EOL: ("\n" | "\r" | "\r\n") >
    43     | <D_SPACE: " " >
    44     | <D_ID: (["0"-"9"])+ >
    45     | <WY: "wy" >
    46     | <ND: "nd" >
    47     | <REL: "rel" >
    48     | <EQ: "=" > : VALUE
    49     | <IDENT: ["a"-"z", "A"-"Z", "_"] (["a"-"z", "A"-"Z", "_", "-", "0"-"9"])* >
    50     | <D_COMMENT_START: "#"> : COMMENT
     39    <C_EOL: ("\n" | "\r" | "\r\n") > : DEFAULT
    5140}
    5241
    5342<VALUE>
    5443TOKEN: {
    55     <V_EOL: ("\n" | "\r" | "\r\n") > : DATA
     44    <V_EOL: ("\n" | "\r" | "\r\n") > : DEFAULT
    5645    | <TEXT: (~["\n", "\r"])+ >
    5746}
     
    6453{
    6554    (
    66         ( p = node() | p = way() | p = relation() )
     55        p = node()
    6756        { primitives.add(p); }
     57    |
     58        p = way()
     59        { primitives.add(p); }
     60    |
     61        p = relation()
     62        { primitives.add(p); }
     63    |
     64        <COMMENT_START> <C_EOL> | <EOL>
    6865    )*
    6966    <EOF>
     
    7976    <NODE>
    8077    { r = new NodeData(); }
    81     id=<H_ID>
     78    ( <WHITESPACE> )+
     79    id=<ID>
    8280    { r.setId(Long.parseLong(id.image)); }
     81    ( <WHITESPACE> )*
    8382    <COLON>
     83    ( <WHITESPACE> )*
    8484    lat=<FLOAT>
     85    ( <WHITESPACE> )*
    8586    <COMMA>
     87    ( <WHITESPACE> )*
    8688    lon=<FLOAT>
    8789    { r.setCoor(new LatLon(Double.parseDouble(lat.image), Double.parseDouble(lon.image))); }
    88     ( <COMMENT_START> <C_EOL> | <H_EOL> )
    89     ( <D_SPACE> <D_SPACE> tag(r) )*
     90    ( <WHITESPACE> )*
     91    ( <COMMENT_START> <C_EOL> | <EOL> )
     92    ( tag(r) )*
    9093    { return r; }
    9194}
     
    98101{
    99102    <WAY>
     103    ( <WHITESPACE> )+
    100104    { r = new WayData(); }
    101     id=<H_ID>
     105    id=<ID>
    102106    { r.setId(Long.parseLong(id.image)); }
    103     ( <COMMENT_START> <C_EOL> | <H_EOL> )
     107    ( <WHITESPACE> )*
     108    ( <COMMENT_START> <C_EOL> | <EOL> )
    104109    way_data(r)
    105110    { return r; }
     
    113118{
    114119    <RELATION>
     120    ( <WHITESPACE> )+
    115121    { r = new RelationData(); }
    116     id=<H_ID>
     122    id=<ID>
    117123    { r.setId(Long.parseLong(id.image)); }
    118     ( <COMMENT_START> <C_EOL> | <H_EOL> )
     124    ( <WHITESPACE> )*
     125    ( <COMMENT_START> <C_EOL> | <EOL> )
    119126    relation_data(r)
    120127    { return r; }
     
    124131{}
    125132{
    126     (
    127         <D_SPACE> <D_SPACE>
    128         ( way_node(r) | tag(r) )
    129     )*
     133    ( LOOKAHEAD(3) way_node(r) | tag(r) )*
    130134}
    131135
     
    135139}
    136140{
     141    ( <WHITESPACE> )+
    137142    <ND>
    138     ( <D_SPACE> )+
    139     id=<D_ID>
     143    ( <WHITESPACE> )+
     144    id=<ID>
    140145    { r.getNodeIds().add(Long.parseLong(id.image)); }
    141     ( <D_SPACE> )*
    142     ( <D_COMMENT_START> <C_EOL> | <D_EOL> )
     146    ( <WHITESPACE> )*
     147    ( <COMMENT_START> <C_EOL> | <EOL> )
    143148}
    144149
     
    146151{}
    147152{
    148     (
    149         <D_SPACE> <D_SPACE>
    150         ( relation_member(r) | tag(r) )
    151     )*
     153    ( LOOKAHEAD(3) relation_member(r) | tag(r) )*
    152154}
    153155
     
    159161}
    160162{
     163    ( <WHITESPACE> )+
    161164    (
    162165        <ND> { type = OsmPrimitiveType.NODE; }
     
    166169        <REL> { type = OsmPrimitiveType.RELATION; }
    167170    )
    168     <D_SPACE>
    169     id=<D_ID>
     171    ( <WHITESPACE> )+
     172    id=<ID>
    170173    (
    171         <D_SPACE>
    172         (
    173             role=<IDENT> ( <D_SPACE> )*
    174         |
    175             ( <D_SPACE> )*
    176         )
     174        LOOKAHEAD(2)
     175        ( <WHITESPACE> )+
     176        role=<IDENT>
    177177    )?
    178178    { r.getMembers().add(new RelationMemberData(role != null ? role.image : "", type, Long.parseLong(id.image))); }
    179     ( <D_COMMENT_START> <C_EOL> | <D_EOL> )
     179    ( <WHITESPACE> )*
     180    ( <COMMENT_START> <C_EOL> | <EOL> )
    180181}
    181182
     
    185186}
    186187{
     188    ( <WHITESPACE> )+
    187189    k=<IDENT>
    188     ( <D_SPACE> ) *
     190    ( <WHITESPACE> )*
    189191    <EQ>
     192    ( <WHITESPACE> )*
    190193    v=<TEXT>
    191194    <V_EOL>
  • applications/editors/josm/plugins/comfort0/test/unit/net/simon04/comfort0/level0l/parsergen/Level0LParserTest.java

    r35288 r35291  
    11package net.simon04.comfort0.level0l.parsergen;
    22
     3import static org.CustomMatchers.hasSize;
    34import static org.hamcrest.CoreMatchers.is;
    45import static org.junit.Assert.assertThat;
    56
    67import java.io.StringReader;
     8import java.util.List;
    79
    810import org.junit.Test;
    911import org.openstreetmap.josm.data.osm.NodeData;
    1012import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
     13import org.openstreetmap.josm.data.osm.PrimitiveData;
    1114import org.openstreetmap.josm.data.osm.RelationData;
    1215import org.openstreetmap.josm.data.osm.WayData;
     
    7376        assertThat(relation.getKeys().size(), is(6));
    7477    }
     78
     79    @Test
     80    public void testExampleFromDocs() throws Exception {
     81        // https://wiki.openstreetmap.org/wiki/Level0L#Examples
     82        final String level0l = "" +
     83                "node 298884269: 54.0901746, 12.2482632 # made by user SvenHRO (46882) in changeset 676636 on 2008-09-21T21:37:45Z\n" +
     84                "node 261728686: 54.0906309, 12.2441924 # versions should be stored server-side\n" +
     85                "node 1831881213: 54.0900666, 12.2539381 # comments are not allowed in tag values, '=' is screened (\\=) only in keys\n" +
     86                "  name = Neu Broderstorf\n" +
     87                "  traffic_sign = city_limit\n" +
     88                "\n" +
     89                "node 298884272: 54.0901447, 12.2516513\n" +
     90                "\n" +
     91                "way 26659127\n" +
     92                "  nd 292403538\n" +
     93                "  nd 298884289\n" +
     94                "  nd 261728686\n" +
     95                "  highway = unclassified\n" +
     96                "  name = Pastower Straße\n" +
     97                "\n" +
     98                "relation 56688 # member types: nd, wy, rel; roles are put after ids\n" +
     99                "  nd 294942404\n" +
     100                "  nd 364933006\n" +
     101                "  wy 4579143 forward\n" +
     102                "  nd 249673494\n" +
     103                "  name = Küstenbus Linie 123\n" +
     104                "  network = VVW\n" +
     105                "  operator = Regionalverkehr Küste\n" +
     106                "  ref = 123\n" +
     107                "  route = bus\n" +
     108                "  type = route\n";
     109        final List<PrimitiveData> primitives = new Level0LParser(new StringReader(level0l)).primitives();
     110        assertThat(primitives, hasSize(6));
     111        assertThat(primitives.get(0).toString(), is("298884269 null V NODE LatLon[lat=54.0901746,lon=12.2482632]"));
     112        assertThat(primitives.get(1).toString(), is("261728686 null V NODE LatLon[lat=54.0906309,lon=12.2441924]"));
     113        assertThat(primitives.get(2).toString(), is("1831881213 [name, Neu Broderstorf, traffic_sign, city_limit] V NODE LatLon[lat=54.0900666,lon=12.2539381]"));
     114        assertThat(primitives.get(3).toString(), is("298884272 null V NODE LatLon[lat=54.0901447,lon=12.2516513]"));
     115        assertThat(primitives.get(4).toString(), is("26659127 [highway, unclassified, name, Pastower Straße] V WAY[292403538, 298884289, 261728686]"));
     116        assertThat(primitives.get(5).toString(), is("56688 [name, Küstenbus Linie 123, network, VVW, operator, Regionalverkehr Küste, ref, 123, route, bus, type, route] V REL [node 294942404, node 364933006, way 4579143, node 249673494]"));
     117    }
     118
    75119}
Note: See TracChangeset for help on using the changeset viewer.