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

Method type

chapter20/src/main/java/com/seaofnodes/simple/Parser.java:785–830  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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

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