Repository navigation
Expand file tree
/
Copy pathHSymbol.java
More file actions
98 lines (86 loc) · 2.17 KB
/
Copy pathHSymbol.java
File metadata and controls
98 lines (86 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
// Copyright (c) 2020, Brian Frank
// Licensed under the Academic Free License version 3.0
//
// History:
// 23 Nov 2020 Matthew Giannini Creation
//
package org.projecthaystack;
/**
* HSymbol is a name to a def in the meta-model namespace.
*/
public class HSymbol extends HVal
{
/**
* Create a symbol from a string.
* @param str The symbol string.
* @return the symbol.
*/
public static HSymbol make(final String str)
{
if (str.isEmpty()) throw new ParseException("Empty str");
if (!Character.isLowerCase(str.charAt(0))) throw new ParseException("Invalid start char: " + str);
int colon = -1;
int dot = -1;
int dash = -1;
for (int i = 0; i< str.length(); ++i)
{
int c = str.charAt(i);
if (c == ':') { if (colon >= 0) throw new ParseException("Too many colons: " + str); colon = i; }
else if (c == '.') { if (dot >= 0) throw new ParseException("Too many dots: " + str); dot = i; }
else if (c == '-') { dash = i; }
else if (!isTagChar(c)) { throw new ParseException("Invalid char at pos: " + i + ": " + str); }
}
if (dot > 0) throw new ParseException("Compose symbols deprecated: " + str);
if (colon > 0) return new HSymbol(str, str.substring( colon+1));
return new HSymbol(str);
}
private static boolean isTagChar(final int c)
{
return Character.isAlphabetic(c) || Character.isDigit(c) || c == '_';
}
private HSymbol(final String str)
{
this(str, str);
}
private HSymbol(final String str, final String name)
{
this.str = str;
this.name = name;
}
/** Internal representation */
private final String str;
private final String name;
/**
*
* @return the simple name.
*/
public String name() { return name; }
@Override
public String toZinc()
{
return '^' + str;
}
@Override
public String toJson()
{
return "y:" + str;
}
@Override
public int hashCode()
{
return str.hashCode();
}
@Override
public boolean equals(Object that)
{
if (!(that instanceof HSymbol)) return false;
final HSymbol x = (HSymbol)that;
return this.str.equals(x.str);
}
@Override
public String toString()
{
return str;
}
}