MCPcopy Create free account
hub / github.com/SeaOfNodes/Simple / type

Method type

chapter18/src/main/java/com/seaofnodes/simple/Parser.java:801–846  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

799 boolean readonly = !writable && match("~");
800 // Only type with a leading `{` is a function pointer...
801 if( peek('{') ) return typeFunPtr();
802
803 // Otherwise you get a type name
804 String tname = _lexer.matchId();
805 if( tname==null ) return posT(old1);
806
807 // Convert the type name to a type.
808 Type t0 = TYPES.get(tname);
809 // No new types as keywords
810 if( t0 == null && KEYWORDS.contains(tname) )
811 return posT(old1);
812 if( t0 == Type.BOTTOM || t0 == Type.TOP ) return t0; // var/val type inference
813 Type t1 = t0 == null ? TypeMemPtr.make(TypeStruct.makeFRef(tname)) :t0; // Null: assume a forward ref type
814 // A qualifier before the name applies to the struct reference, not
815 // to an enclosing array. Arrays have their own [] / [~] qualifier.
816 if( t1 instanceof TypeMemPtr tmp ) {
817 if( readonly || !writable ) t1 = tmp.makeRO();
818 } else if( writable || readonly )
819 return posT(old1);
820
821 // Nest arrays and '?' as needed
822 Type t2 = t1;
823 while( true ) {
824 if( match("?") ) {
825 if( !(t2 instanceof TypeMemPtr tmp) )
826 throw error("Type "+t0+" cannot be null");
827 if( tmp.nullable() ) throw error("Type "+t2+" already allows null");
828 t2 = tmp.makeNullable();
829 } else if( match("[~]") ) {
830 TypeMemPtr ary = typeAry(t2);
831 t2 = ary.makeFrom(ary._obj.makeRO()).makeRO();
832 } else if( match("[]") ) {
833 t2 = typeAry(t2);
834 } else
835 break;
836 }
837
838 // Check no forward ref
839 if( t0 != null ) return t2;
840 // Check valid forward ref, after parsing all the type extra bits.
841 // Cannot check earlier, because cannot find required 'id' until after "[]?" syntax
842 int old2 = pos();
843 if( !match("!") ) match("~");
844 String id = _lexer.matchId();
845 if( !(peek(',') || peek(';')) )
846 return posT(old1);
847 pos(old2); // Reset lexer to reparse
848 if( id==null )
849 return posT(old1); // Reset lexer to reparse

Callers 4

typeFunPtrMethod · 0.95
allocMethod · 0.95
funcMethod · 0.95

Calls 15

posMethod · 0.95
peekMethod · 0.95
typeFunPtrMethod · 0.95
posTMethod · 0.95
makeMethod · 0.95
makeFRefMethod · 0.95
matchMethod · 0.95
errorMethod · 0.95
typeAryMethod · 0.95
matchIdMethod · 0.45
getMethod · 0.45
containsMethod · 0.45

Tested by

no test coverage detected