Bug 6868: If-feature argument may be boolean expression
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / spi / source / DeclarationInTextSource.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.parser.spi.source;
9
10 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
11
12 /**
13  *
14  * Reference of statement source present in textual source format
15  *
16  * Utility implementation of {@link StatementSourceReference} for textual sources,
17  * this is prefered {@link StatementSourceReference} for implementations
18  * of YANG / YIN statement stream sources.
19  *
20  *
21  *  To create source reference use one of this static factories:
22  *  <ul>
23  *  <li>{@link #atPosition(String, int, int)} - provides most specific reference of statement location,
24  *  this is most prefered since it provides most context to debug YANG model.
25  *  </li>
26  *  <li>{@link #atLine(String, int)}- provides source and line of statement location.
27  *  </li>
28  *  <li>{@link #inSource(String)} - least specific reference, should be used only if any of previous
29  *  references are unable to create / derive from source.
30  *  </li>
31  *  </ul>
32  *
33  */
34 public abstract class DeclarationInTextSource implements StatementSourceReference {
35
36     private final String sourceName;
37
38     DeclarationInTextSource(final String sourceName) {
39         this.sourceName = sourceName;
40     }
41
42     public String getSourceName() {
43         return sourceName;
44     }
45
46     @Override
47     public StatementSource getStatementSource() {
48         return StatementSource.DECLARATION;
49     }
50
51     @Override
52     public abstract String toString();
53
54     public static DeclarationInTextSource inSource(final String sourceName) {
55         return new InSource(sourceName);
56     }
57
58     public static DeclarationInTextSource atLine(final String sourceName, final int line) {
59         return new AtLine(sourceName, line);
60     }
61
62     public static DeclarationInTextSource atPosition(final String sourceName, final int line, final int position) {
63         return new AtPosition(sourceName, line,position);
64     }
65
66     private static class InSource extends DeclarationInTextSource {
67         InSource(final String sourceName) {
68             super(sourceName);
69         }
70
71         @Override
72         public String toString() {
73             return getSourceName();
74         }
75     }
76
77     private static class AtLine extends InSource {
78
79         private final int line;
80
81         AtLine(final String sourceName, final int line) {
82             super(sourceName);
83             this.line = line;
84         }
85
86         @Override
87         public String toString() {
88             return getSourceName() + ':' + line;
89         }
90
91         public int getLine() {
92             return line;
93         }
94     }
95
96     private static class AtPosition extends AtLine {
97
98         private final int character;
99
100         AtPosition(final String sourceName, final int line, final int character) {
101             super(sourceName, line);
102             this.character = character;
103         }
104
105         @Override
106         public String toString() {
107             return getSourceName() + ':' + getLine() + ':' + character;
108         }
109     }
110 }